user1107922
user1107922

Reputation: 610

android use asynctask to parse json

I am working on android application and I need to parse my json object with data. How you can see I create JSONParser class and try to use asynctask but there is something wrong and I can't understand where is the problem. Every time I use it resultJSON is null. Hope that you can give me an advice!

public class JSONParser {
    private String resultJSON;

public JSONArray getJSON(String url) throws JSONException {
    Parser parser = new Parser();
    parser.execute(url);
    return json;
}

private class Parser extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        for (String url : urls) {
            StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse response = client.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                    resultJSON = builder.toString();
                } else {
                    Log.e(JSONParser.class.toString(), "Failed to download file");
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
      }
      return resultJSON;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        try {
            json = new JSONArray(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
}

Upvotes: 2

Views: 3473

Answers (3)

user1107922
user1107922

Reputation: 610

The problem is fixed. It's an awful workaround but it works. Add this line

while(json==null) {}

after calling the execute method.

Upvotes: 0

Code_Life
Code_Life

Reputation: 5892


Why don't you JSONArray json = new JSONArray(resultJSON); do this on post execute method of async task .

And i will not suggest varevarao way , as it will create extra burden of one thread .

Upvotes: 1

varevarao
varevarao

Reputation: 2186

You should use the get() method of the AsyncTask class to retrieve the result of the task. It waits for the task to complete and gets the result (which means it'd be best if you enclose it within a separate thread with a progress dialog or just a background thread).

public JSONArray getJSON(String url) throws JSONException {
    Parser parser = new Parser();
    parser.execute(url);
    resultJSON = parser.get(); // Probably put this in a Thread to avoid spending too much time waiting for a result on the main thread
    JSONArray json = new JSONArray(resultJSON);
    return json;
}

Upvotes: 0

Related Questions