简体   繁体   中英

Android JSONArray NullPointerException Cannot Evaluate JSONArray.toString()

I'm building my first Android app and it requires pulling is JSON formatted data from a local web server. The web server is up and running and I can see the returned data while debugging the Android app. However, while trying to parse the data into a JSON Array, I keep hitting a NullPointerException at the last line of code.

The call:


        JSONParserTask jParser = new JSONParserTask();
        ArrayList> ResultsList = new ArrayList>();
        EditText etSoNo = (EditText) findViewById(R.id.txtSoNo);
        //String url = "http://xxx.xxx.xxx.xxx/api/SOLocLookup/" + etSoNo.getText();
        String url = "http://headers.jsontest.com/";

        try{
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpGet httpget = new HttpGet(url);
            httpget.setHeader("Content-type", "application/json");
            httpget.setHeader("Accept", "JSON");

            InputStream inputStream = null;
            String result = null;

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            result = sb.toString();

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            JSONArray jArray = new JSONArray(result);

At first I thought that maybe my API wasn't sending out a JSON formatted response, so you can see that I changed the call to a test JSON service: http://headers.jsontest.com/

The return data:

{ "Accept-Language": "en-US,en;q=0.8", "Host": "headers.jsontest.com", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8" }

My LogCat:

09-26 18:08:30.977 61-214/system_process I/ActivityManager: Starting: Intent { cmp=com.evs.sodscamera/.activity.SoMatlLookup.SoMatlLookup } from pid 1034 09-26 18:08:31.997 61-99/system_process I/ActivityManager: Displayed com.evs.sodscamera/.activity.SoMatlLookup.SoMatlLookup: +913ms 09-26 18:09:11.748 1034-1034/com.evs.sodscamera D/dalvikvm: JDWP invocation returning with exceptObj=0x40589210 (Ljava/lang/NullPointerException;)

I'm really at a loss. I'm sure that it's something so simple, but with my inexperience I just can't find it.

Thanks!!

-Greg

Update 1:

Changing the JSONArray to a JSONObject works for the data returned from the test website. But when I go to retrieve my original data, it actually is stored as an Array. I should have realized that earlier and not posted using bad test data. My apologies.

New Return Data:

[{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\\"","Size1":48.00000,"Size2":144.00000,"TagNo":62336.0,"Pcs":1.0,"Loc":"G0303"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\\"","Size1":48.00000,"Size2":144.00000,"TagNo":67931.0,"Pcs":124.0,"Loc":"G0303"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\\"","Size1":48.00000,"Size2":48.00000,"TagNo":68912.0,"Pcs":1.0,"Loc":"REM75"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\\"","Size1":48.00000,"Size2":144.00000,"TagNo":69589.0,"Pcs":177.0,"Loc":"RECVG"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\\"","Size1":48.00000,"Size2":48.00000,"TagNo":76493.0,"Pcs":1.0,"Loc":"REM75"},{"SoNo":119152.0,"SoLn":2.00,"ItemID":"2024SHA3125M","ItemDesc":"2024-T3 ALCLAD SHEET .125\\" MRS","Size1":48.00000,"Size2":144.00000,"TagNo":72070.0,"Pc s":12.0,"Loc":"H0108"},{"SoNo":119152.0,"SoLn":3.00,"ItemID":"177SH-016","ItemDesc":"T17/7 SHEET .016\\"
AMS-5528","Size1":36.00000,"Size2":120.00000,"TagNo":70031.0,"Pcs":1.0,"Loc":"S0504"},{"SoNo":119152.0,"SoLn":3.00,"ItemID":"177SH-016","ItemDesc":"T17/7 SHEET .016\\"","Size1":36.00000,"Size2":120.00000,"TagNo":76108.0,"Pcs":10.0,"Loc":""}]

Thanks!!

Change this

  JSONArray jArray = new JSONArray(result);

To

  JSONObject jobject = new JSONObject(result);

What you get is a JSONObject

{ // represents json object node
   "Accept-Language": "en-US,en;q=0.8",
   "Host": "headers.jsontest.com",
   "Referer": "http://stackoverflow.com/questions/19036091/android-jsonarray-nullpointerexception-cannot-evaluate-jsonarray-tostring",
   "User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36",
   "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}    

Also use a thread or Asynctask if you are getting json from a server. All network related operation should be done on a thread

What you are trying to parse is not an array it is just JSONObject .

Arrays in JSON are denoted with []. Eg this would be an array:

[{"key": "value", "key2": "value2"}]

And this is the single object contained in it:

{"key": "value", "key2": "value2"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM