1

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.

4
  • can try ? BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); } May 31, 2012 at 10:27
  • can you set the debug point at string = br.readLine(); at check flow is reahig there or nor ? May 31, 2012 at 10:28
  • I have. I'll update the question. May 31, 2012 at 10:29
  • Since I do not have sufficient experience in Android, I am writing this as a comment. Are you sure that Url.openStream is returning the correct result? Try using an HttpClient instead. See my previous question on how to implement it. stackoverflow.com/questions/10757363/… May 31, 2012 at 10:30

2 Answers 2

1

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");
        }
2
  • The permission is there. I've updated the question. Thanks for the performance advice :-) May 31, 2012 at 10:32
  • Please put a Log as the first line of your doInBackground (so we are pretty sure that you enter in it). Please also use this code to read data from your InputStream and try it :) (updated my answer)
    – StErMi
    May 31, 2012 at 10:40
0

oehm... you are creating your task with an activity object (this) (???)

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

but your task expects a View

public JSONParser(ListFragment fragment) {

im surprised that this doesnt throw an exception

3
  • I'm calling the task from a ListFragment. No problems there. May 31, 2012 at 10:37
  • you are calling it from a ListFrament? oehm... how? in an eventlistener?
    – CAA
    May 31, 2012 at 10:40
  • public class TestFragment extends ListFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AsyncTask task = new JSONParser(this).execute("10.0.2.2:1822/webservice/gettools.aspx");}} May 31, 2012 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.