thyzz
thyzz

Reputation: 724

How to keep the data value and return the data to the main UI, after Asyntask is done?

I'm trying to get the value from AsyncTask class to return to the main UI, even after the AsyncTask class is completed. The string value from onPostExectute() to be saved into json_string for latter use. However, when i tried running this code, it gives me NullPointerException.

I've been trying my best for 3 days now, still couldn't figure out how to solve this :( someone please help. I really appreciate your help.

P.S: I followed the tutorial from http://www.androidhive.info/2012/01/android-json-parsing-tutorial/, however I made this simple version of that to test.

public class ParsingJSONDataSample extends Activity {

    // url to make request
    private static String url = "http://api.androidhive.info/contacts/";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";

    static TextView textId, textName;

    // contacts JSONArray
    static JSONArray contacts = null;

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json_string;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.json_listview_sample);

        textId = (TextView) findViewById(R.id.textView1);
        textName = (TextView) findViewById(R.id.textView2);

        new ReadJSONWebServiceFeedFromURL().execute(url);

        jObj = new JSONObject(json_string);

        // Getting Array of Contacts
        contacts = jObj.getJSONArray(TAG_CONTACTS);
        JSONObject c = contacts.getJSONObject(0);
        textId.setText(c.getString(TAG_ID));
        textName.setText(c.getString(TAG_NAME));        
        }
    }

    //get the JSON webservice from URL
    public String getJSONFromUrl(String url) {

        StringBuilder sb = new StringBuilder();
        String json;
        // Making HTTP request
        try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                StatusLine statusLine = httpResponse.getStatusLine();
                int statuscode = statusLine.getStatusCode();

                if (statuscode == 200){
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent(); 

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                    String line = null;

                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    json = sb.toString();
                }
                else{
                    Log.d("readJSONFeed", "Failed to download file");
                }

        } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
        } catch (ClientProtocolException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }

        // return JSON String
        return json;
    }

    //Asynctask class to read the JSON webservice in background
    private class ReadJSONWebServiceFeedFromURL extends AsyncTask<String, Void, String>{

        protected String doInBackground(String... urls) {
            // TODO Auto-generated method stub
            return getJSONFromUrl(urls[0]);
        }

        protected void onPostExecute(String result){
            json_string = result;
        }
    }
}

Upvotes: 3

Views: 129

Answers (1)

MSA
MSA

Reputation: 2482

String json; use it as global

Cheers,

Upvotes: 1

Related Questions