Suyash
Suyash

Reputation: 3

Android AsyncTask with JSON Parsing error

Error coming in this section:

protected void onPostExecute(JSONObject json) {
    try {
        // Getting JSON Array
        user = json.getJSONArray(TAG_USER);
        JSONObject c = json.getJSONObject(0);
        String id = c.getString(TAG_ID);
        String name = c.getString(TAG_NAME);
        Log.i("id",id);
    }
}

------- Webservice Result---

{"GetDataResult":{"ID":8,"Name":"Foo Bar"}} Working Link - http://127.0.0.1/WcfService4/Service1.svc/getData/?key=8

Provide the better solution for solving this.

Upvotes: 0

Views: 81

Answers (2)

Sumeet Rathore
Sumeet Rathore

Reputation: 232

It seems you havn't use your JSONArray object

JSONArray mainfoodlist = null;


    tipshealth = json.getJSONArray(TAG_RESPONSE);

        // looping through All RESPONSE
        for (int i = 0; i < tipshealth.length(); i++) {
        JSONObject jsonobj = tipshealth.getJSONObject(i);
        tipHealth = jsonobj.getString(KEY_HEALTHTIPS);

        listhealthtips.add(tipshealth.getJSONObject(i).getString("tips"));

        }

Upvotes: 1

Jack Xu
Jack Xu

Reputation: 75

It seems like that the problem comes from this section:

user = json.getJSONArray(TAG_USER);
JSONObject c = json.getJSONObject(0);

you get a JOSNObject user but you never used it.

I guess this is probably what you mean:

user = json.getJSONArray(TAG_USER);
JSONObject c = user.getJSONObject(0);

Upvotes: 0

Related Questions