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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:25:28+00:00 2026-06-04T19:25:28+00:00

I have an asynctask that – when executed – is cancelled right away. My

  • 0

I have an asynctask that – when executed – is cancelled right away.

My class looks like this:

public class JSONParser extends AsyncTask<String, Void, JSONArray> {
    private ListFragment fragment;
    @Override
    protected JSONArray doInBackground(String... strings) {
        JSONArray ja = null;
        String string = "";
        try {
            URL url = new URL(strings[0]);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            string = br.readLine();

            ja = new JSONArray(string);
        } catch (Exception e ) {
            Log.w("Special", e.toString());
        }
        return ja;
    }

    public JSONParser(ListFragment fragment) {
        this.fragment = fragment;
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {
        try {
        ArrayList<HashMap<String ,String>> datalist = new ArrayList<HashMap<String, String>>();

        int i = 0;
        while (i < jsonArray.length()) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject tool = jsonArray.getJSONObject(i);

            map.put("id", tool.getInt("Id") + "");
            map.put("name", tool.getString("Name"));
            datalist.add(map);
            i++;
        }
        fragment.setListAdapter(new SimpleAdapter(fragment.getActivity(), datalist, R.layout.tools_list, new String[] {"name"}, new int[] {R.id.text}));
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

and from my fragment I’m calling it like this

AsyncTask task = new JSONParser(this).execute("http://10.0.2.2:1288/webservice/gettools.aspx");

Using the debugger I can see that as soon as the constructor is called, it skips to onCancelled() and returns. The URL is valid and working, I get no messages in the Log, and the JSON is valid.

Update: Also I have the required permission and OnCancelled() is called before it enters doInBackground(). doInBackground() is never called.

Any ideas?

I’m using IntelliJ and an AVD with Android 4.0.3.

  • 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-04T19:25:31+00:00Added an answer on June 4, 2026 at 7:25 pm

    Do you have set Internet permissions in your AndroidManifest?
    Are you sure that in the doInBackground there is no exception fired?

    Maybe it could be a better idea to also put the while (i < jsonArray.length()) part in the doInBackground 🙂 (for performance)

    Code to read String:

    BufferedReader reader = new BufferedReader(yourInputStreamReader,8);
    
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have an Activity Class with an inner protected class that extends AsyncTask. I
I have an Activity class which has an attribute that references an AsyncTask instance
I have classic AsyncTask to load image: private class DownloadImageTask extends AsyncTask<String,Void,Bitmap> { Bitmap
I have an AsyncTask with a textview loaded in the class so it looks
I have a service, that requests another class, which launches an AsyncTask Service->Weather Class->Execute
I have an AsyncTask that is executed before I move on to the next
i have an AsyncTask that gets executed when the app starts up. on a
I have an AsyncTask that handles a rather long download, using HttpClient. I'd like
I have a class which extends AsyncTask, which is intended to serve as a

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.