Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:53:22+00:00 2026-06-14T12:53:22+00:00

I have a class in which I have written a AsyncTask to get Json

  • 0

I have a class in which I have written a AsyncTask to get Json from net.

public class MyAsyncGetJsonFromUrl extends AsyncTask<Void, Void, JSONObject> {
    Context context_;
    String url_;
    List<NameValuePair> params_;

    public MyAsyncGetJsonFromUrl(Context context_, List<NameValuePair> params_, String url_) {
        this.context_ = context;
        this.params_ = params_;
        this.url_ = url_;
    }

    @Override
    protected JSONObject doInBackground(Void... params) {
        JSONObject jObj_ = null;
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url_);
            httpPost.setEntity(new UrlEncodedFormEntity(params_));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is_ = httpEntity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is_.close();
            String json_ = sb.toString();
            Log.e("JSON", json);

            jObj_ = new JSONObject(json_);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jObj_;
    }

    protected void onPostExecute(JSONObject jObj_) {
        jObj = jObj_;
    }
}

Also in this class there is a method which returns this Json Object to Activity who is calling the method.

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
    new MyAsyncGetJsonFromUrl(context, params, url).execute();
    return jObj;
}

Now the problem is after starting AsyncTask new MyAsyncGetJsonFromUrl(context, params, url).execute(); immediately the line return jObj; is called and null is returned.
So I want to stop the execution until Async task is complete.
And please don’t mention this question as duplicate, because no other question is exactly same as this scenario

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:53:23+00:00Added an answer on June 14, 2026 at 12:53 pm

    Async tasks are asynchronous. This means that they are executed in the background independent of the programs current thread (they run in a background thread). So you should not try to stop execution of your program until async task is complete.

    So you have 2 options:

    • Run your code on the UI thread itself instead of in an async task

    • Assign your value from the async tasks onPostExecute() method

    If you decide to go with an async task and go with option B, then you can have some static variable or something like that to which the value of the JSON object can be assigned at the end of the async task and can be accessed later. You can also do whatever post processing you need to do with the JSON object in the onPostExecute() method itself (for ex: parse the object and display it to the user) since this method is running on the UI thread after the async task completes its operations in the background thread.

    If you need a return value from your async task, you can do something like:

    jObj = new MyAsyncGetJsonFromUrl(context, params, url).execute().get(); 
    

    In this case, the program will wait for the computation to complete except if there is an exception such as the thread being interrupted or cancelled (due to memory constraints etc). But this approach is generally not advised. The point of an async task is to allow asynchronous operations and your program execution should not be stopped because of it.

    The return values will be passed from doInBackground() as a parameter to the OnPostExecute() method. You don’t have to put a return value in onPostExecute since it’s executing on the UI thread itself. Just use the result and do what you need to with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a base class from which I wish to derive several child
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
i have written a class which implements ListFieldCallBack like, import java.util.Vector; import net.rim.device.api.ui.Graphics; import
I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is
I have a class written in c# which is acting as a wrapper around
I have an object of class Message, which can be written and subsequently updated.
have written this little class, which generates a UUID every time an object of
I have written an Apex class in a Salesforce managed package which uses the
I have class which have one public method Start , one private method and
I have class Money which is an @Embeddable @Embeddable public class Money implements Serializable,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.