从Java(Android)检索JSONArrays

时间:2011-12-23 01:23:52

标签: php android arrays

我正在尝试解析从php收到的JSONArray。我从我的日志猫看到我得到的值是[[“84.0918544152744”],[“84.0918544152744”]]但是当我去解析数组中的元素时,我得到一个NullPointerException。这是我的代码:

 private class SetAverages extends AsyncTask<Void, Void, BufferedReader> {
         @Override
         protected BufferedReader doInBackground(Void... params) {
             String url = "...";

             Log.i(LOG_TAG, "URL = " + url);
             URL iuri;
             try {
                 iuri = new URL(url);
                 URLConnection connection = iuri.openConnection();
                 connection.setDoInput(true);
                 connection.setDoOutput(true);
                 connection.setUseCaches(false);
                 connection.setRequestProperty("Content-type",
                         "application/x-www-form-urlencoded");
                 BufferedReader br = new BufferedReader(new InputStreamReader(
                         (InputStream) connection.getContent()));
                 Log.i(LOG_TAG, "br value is " + br.readLine());
                return br;
             } catch (MalformedURLException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
           return null;
         }

         @Override
         protected void onPostExecute(BufferedReader br) {
             if(br == null){
                 Log.v(LOG_TAG, "br from set averages is null");
                 return;
             }

             Log.v(LOG_TAG, "Get Averages onPostExecute");
             try{

                 Log.i(LOG_TAG, "br value is " + br.toString());
                 Log.i(LOG_TAG, "about to set up json array....");

                 JSONObject inputjson = new JSONObject(br.readLine());
                 //JSONArray inputjson = new JSONArray(br.readLine());
                 Log.i(LOG_TAG, "this worked....");

                 Log.i(LOG_TAG, "size of inputjson = " + inputjson.length());
                return;
            }catch(NullPointerException e){
                 Log.e(LOG_TAG, "Unable to get averages, NullPointerException");
                 e.printStackTrace();
             }catch(NumberFormatException e){
                 Log.e(LOG_TAG, "Unable to get averages, Number Format Exception");
                 e.printStackTrace();
             } catch (JSONException e) {
                 Log.e(LOG_TAG, "JSON Exception");
                 e.printStackTrace();
             } catch (IOException e) {
                 Log.e(LOG_TAG, "IO Exception");
                 e.printStackTrace();
             }

             return;
         }

以下是logcat的摘录:

12-22 20:07:59.345: INFO/HA(1000): br value is [["84.0158352941176"],["84.0158352941176"]]
12-22 20:07:59.355: VERBOSE/HA(1000): Get Averages onPostExecute
12-22 20:07:59.355: INFO/HA(1000): br value is java.io.BufferedReader@44f8f0c0
12-22 20:07:59.355: INFO/HA(1000): about to set up json array....
12-22 20:07:59.355: ERROR/HA(1000): Unable to get averages, NullPointerException

1 个答案:

答案 0 :(得分:0)

BufferedReader br = new BufferedRe ..                   Log.i(LOG_TAG,“br值为”+ br.readLine());

这里你正在使用行缓冲区,所以当你到达这里时:

              JSONObject inputjson = new JSONObject(br.readLine());

你不再有一个字符串。

尝试删除此行Log.i(LOG_TAG,“br值为”+ br.readLine());

祝你好运!