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 6632009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:37:34+00:00 2026-05-25T22:37:34+00:00

How do I get the data from my AsyncTask? My MainActivity is calling the

  • 0

How do I get the data from my AsyncTask? My MainActivity is calling the DataCall.getJSON function that triggers the AsyncTask but I am not sure how to get the data back to the original Activity.

MainActivity with call to DataCall that should return a string and save it in state_data

String state_data =  DataCall.getJSON(spinnerURL,spinnerContentType); 

DataCall:

public class DataCall extends Activity {
    private static final String TAG = "MyApp";


    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {


        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }


        protected void onPostExecute(String result) {
            //THIS IS WHERE I NEED TO RETURN MY DATA TO THE MAIN ACTIVITY. (I am guessing)
        }

        }

    public void getJSON(String myUrlString, String contentType) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.mywebsite.com/" + myUrlString });

    }

}
  • 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-05-25T22:37:35+00:00Added an answer on May 25, 2026 at 10:37 pm

    The key for me was to create a class called URLWithParams or something because AsyncTask will allow only 1 type to be sent IN, and I needed both the URL and the params for the HTTP request.

    public class URLWithParams {
    
        public String url;
        public List<NameValuePair> nameValuePairs;
    
        public URLWithParams()
        {
            nameValuePairs = new ArrayList<NameValuePair>();
        }
    }
    

    and then I send it to a JSONClient:

    public class JSONClient extends AsyncTask<URLWithParams, Void, String> {
        private final static String TAG = "JSONClient";
    
        ProgressDialog progressDialog ;
        GetJSONListener getJSONListener;
        public JSONClient(GetJSONListener listener){
            this.getJSONListener = listener;
        }
    
        @Override
        protected String doInBackground(URLWithParams... urls) {
            return connect(urls[0].url, urls[0].nameValuePairs);
        }
    
        public static String connect(String url, List<NameValuePair> pairs)
        {
            HttpClient httpclient = new DefaultHttpClient();
    
            if(url == null)
            {
                Log.d(TAG, "want to connect, but url is null");
            }
            else
            {
                Log.d(TAG, "starting connect with url " + url);
            }
    
            if(pairs == null)
            {
                Log.d(TAG, "want to connect, though pairs is null");
            }
            else
            {
                Log.d(TAG, "starting connect with this many pairs: " + pairs.size());
                for(NameValuePair dog : pairs)
                {
                    Log.d(TAG, "example: " + dog.toString());
                }
            }
    
            // Execute the request
            HttpResponse response;
            try {
                // Prepare a request object
                HttpPost httpPost = new HttpPost(url); 
                httpPost.setEntity(new UrlEncodedFormEntity(pairs));
                response = httpclient.execute(httpPost);
                // Examine the response status
                Log.i(TAG,response.getStatusLine().toString());
    
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                String json = reader.readLine();
                return json;
    
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return null;
        }
    
    
    
        @Override
        protected void onPostExecute(String json ) {
            getJSONListener.onRemoteCallComplete(json);
        }
    
    
        public interface GetJSONListener {
            public void onRemoteCallComplete(String jsonFromNet);
        }
    
    }
    

    Then call it from my main class like this

    public class BookCatalog implements GetJSONListener {
        private final String TAG = this.getClass().getSimpleName();
    
        private String catalog_url = "URL";
    
        private void getCatalogFromServer() {
    
            URLWithParams mURLWithParams = new URLWithParams();
            mURLWithParams.url = catalog_url;
    
            try {
                JSONClient asyncPoster = new JSONClient(this);
                asyncPoster.execute(mURLWithParams);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        @Override
        public void onRemoteCallComplete(String jsonBookCatalogList) {
    
            Log.d(TAG, "received json catalog:");
            Log.d(TAG, jsonBookCatalogList);
        JSONObject bookCatalogResult;
        try {
            bookCatalogResult = (JSONObject) new JSONTokener(jsonBookCatalogList).nextValue();
            JSONArray books = bookCatalogResult.getJSONArray("books");
    
            if(books != null) {
                ArrayList<String> newBookOrdering = new ArrayList<String>();
                int num_books = books.length();
                BookCatalogEntry temp;
    
                DebugLog.d(TAG, "apparently we found " + Integer.toString(num_books) + " books.");
                for(int book_id = 0; book_id < num_books; book_id++) {
                    JSONObject book = books.getJSONObject(book_id);
                    String title = book.getString("title");
                    int version = book.getInt("price");
                }
            }
    
        } catch (JSONException e) {
            e.printStackTrace();
        } 
    
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get data from a 3rd party API that just gives me back a
I have a listview that's populated by rows that get their data from a
I get data from a database in YYYY-mm-dd format, but I want to show
I'm trying to get data from a website- xml. Everything works fine. But the
I am currently having trouble getting a value from an AsyncTask that gets data
I am using an Intent to Broadcast data from a AsyncTask thread back to
We get data from one of our partners that is running an i5 AS/400
I'm making an application that get data from webservice , insert it into a
I have an AsyncTask, that fills a custom List with parsed data from Internet.
I am using AsyncTask to get data from a server and want to show

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.