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

  • SEARCH
  • Home
  • 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 7928057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:38:18+00:00 2026-06-03T19:38:18+00:00

So I have this loader class which extends AsyncTask. Then I do new loader().execute();

  • 0

So I have this loader class which extends AsyncTask. Then I do new loader().execute(); but I want to use the JSONArray response which my loader class returns how do I do that? Because I need it in several different places? Or should I just move my code to onPostExecute and do everything from there?

public class loader extends AsyncTask<String, Integer, JSONArray> {

    ProgressDialog dialog;

    protected void onPreExecute() {

        dialog = ProgressDialog.show(ChallengeList.this, "", "Laddar...");
        dialog.setCancelable(true);
    }

    @Override
    protected JSONArray doInBackground(String... params) {


    JSONArray response = null;
    HttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(listURL);

    try {

        HttpResponse resp = client.execute(httppost);
        StatusLine statusLine = resp.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        Log.i("Statuscode", "statusCode"+statusCode);
        if (statusCode == 200) {
            final JSONObject json = new JSONObject();

            json.put("userID", prefs.id());

            response = SendHttp.parseHttp(listURL, json);

        }
    } catch (JSONException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } 

        return response;
    }
    protected void onPostExecute(JSONArray result) {
        dialog.dismiss();
    }
}
  • 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-03T19:38:19+00:00Added an answer on June 3, 2026 at 7:38 pm

    The method onPostExecute has as parameter the JSONArray you returned from the doInBackground method.

    onPostExecute runs on your main (caller) activity’s thread, so besides dismissing the dialog in that method, you can process the result array further, pass it safely to other methods, etc:

    @Override
    protected void onPostExecute(JSONArray result)
    {
        super.onPostExecute(result);
        final Message msg = new Message();
        msg.obj = result;
        if (youWantToUseHandler)
            handler.dispatchMessage(msg);
        else
            writeJSONArray(result);
    }
    

    the handler:

    final Handler handler = new Handler()
    {
        public void handleMessage(Message msg) 
        {
            final JSONArray result = (JSONArray)msg.obj;
            writeJSONArray(result);
        };
    };
    

    Some other method:

    private void writeJSONArray(final JSONArray result)
    {
        for (int i = 0; i < result.length(); i++)
        {
            try
            {
                Log.d("SAMPLE", result.get(i).toString());
            }
            catch (JSONException e)
            {
                Log.e("SAMPLE", "error getting result " + i, e);
            }
        }
    }
    

    Since onPostExecute “Runs on the UI thread after doInBackground. The specified result is the value returned by doInBackground or null if the task was cancelled or an exception occured.” ~API Docs
    You can call any method you’ve declared in your class, and pass this array as a parameter to it.

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

Sidebar

Related Questions

I have this custom class which extends EventDispatcher private var assetsManager:AssetManager; And this running
I have a class called 'Items' to which 'Equips' extends from and 'Helmet' then
I have a class which extends MapActivity. I have pins which I place on
I have a User class marked as entity which also implements UserDetails. I want
I have a custom view (which extends GLSurfaceView) I'm trying to use in my
I have this model at app/models/product.php : class Product extends AppModel { var $hasAndBelongsToMany
I have this code: public static final <TypeVO extends BaseVo> List<SelectItem> populateSelectBoxForType( final Class<TypeVO>
I have one main class main, which I use to store the configuration and
I have an ajax loader much like this one: $('#loadingDiv') .hide() // hide it
I have this XAML: <Window x:Class=WpfBindToCodeBehind.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=Window1 Height=300 Width=300 Loaded=Window_Loaded> <StackPanel Orientation=Vertical>

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.