intrepidkarthi
intrepidkarthi

Reputation: 3102

JSONException inside AsyncTask in Android

Here is my AsyncTask. Getting JSONException inside the doInbackground method.

 try {
    Log.v("thisurl", url);
    JSONArray data_holder = new JSONArray();
    JSONParser jParser = new JSONParser();
    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);
            data_holder = json.getJSONArray(data_val);
    for (int i = 0; i < data_holder.length(); i++) {
        HashMap<String, String> data_map = new HashMap<String, String>();
        JSONObject obj = data_holder.getJSONObject(i);
             }
      }

Catching the exception. But I am unable to find the reason from that. The URL fetches the json response correctly when I check that manually. Here is my stack trace.

 W/System.err(3353): org.json.JSONException: No value for searchresult
 W/System.err(3353):    at org.json.JSONObject.get(JSONObject.java:354)
 W/System.err(3353):    at org.json.JSONObject.getJSONArray(JSONObject.java:544)
 W/System.err(3353):    at com.store.SearchResultActivity$getJsonProductData.doInBackground(SearchResultActivity.java:318)
 W/System.err(3353):    at com.store.SearchResultActivity$getJsonProductData.doInBackground(SearchResultActivity.java:1)
 W/System.err(3353):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
 W/System.err(3353):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 W/System.err(3353):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 W/System.err(3353):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 W/System.err(3353):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 W/System.err(3353):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 W/System.err(3353):    at java.lang.Thread.run(Thread.java:856)
 D/AndroidRuntime(3353): Shutting down VM
 W/dalvikvm(3353): threadid=1: thread exiting with uncaught exception (group=0x40ccb300)

Can anyone point out what I am doing wrongly here?

Upvotes: 2

Views: 301

Answers (1)

jeet
jeet

Reputation: 29199

Error is on line

 data_holder = json.getJSONArray(data_val);

and its because, there is no jsonarray exists with key data_val. check response, and value of data_val. if these are matching properly, or not, also just for a note, key value are case sensitive.

Upvotes: 2

Related Questions