JSON读取错误

时间:2014-03-05 14:15:25

标签: android json

我正在创建一个应用程序,其中一切正常但是当前端活动中的代码调用JSONReader类的Read方法时,它会抛出错误。我从互联网和我的同事那里交叉检查了阅读JSON的代码,但他们说它很好。我无法找到确切的错误。

我尝试将LOG放在我怀疑的每个地方,但没有任何帮助,并抛出NULL POINTER EXCEPTION,因为主读取器不工作且没有数据被提取。

任何人都可以提供帮助。

这是JSON Reader类和AsynkTask

JSONReader.java

public class JSONReader {

    JSONObject json = null;

    public JSONObject JSONRead(String url){

        try
        {
            StringBuilder jsonStr = new StringBuilder();
            InputStream is = null;

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpentity = httpResponse.getEntity();

            is = httpentity.getContent();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
            String line;

            while((line = buffer.readLine()) != null){

                jsonStr.append(line + "\n");
            }

            json = new JSONObject(jsonStr.toString());
            is.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        System.out.println(">>>>>" + json.toString());
        return json;
    }

}

AsynkTask

@TargetApi(Build.VERSION_CODES.CUPCAKE)
    private class JSONParse extends AsyncTask<String, String, JSONObject> 
    {

        private ProgressDialog pDialog;
        private String value;
        private String valueimg;

        @TargetApi(Build.VERSION_CODES.CUPCAKE)
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(WeatherHome.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            JSONReader jsonr = new JSONReader();

            JSONObject jo = jsonr.JSONRead(url);
            Log.e(">>>>", ">>>>>" + jo.toString());
            return jo;
        }

        @Override
        protected void onPostExecute(JSONObject json) 
        {
            pDialog.dismiss();

            try 
            {
                Log.e(">>>>>>>>>>> On Post Execute", ">>>>>>>>>." + json);
                HashMap<String, String> hmap = new HashMap<String, String>();
                JSONObject jObj = json.getJSONObject(DATA);
                JSONArray jArr = jObj.getJSONArray(WEATHER);

                for (int i = 0; i < jArr.length(); i++) {



                    json = jArr.getJSONObject(i);
                    String date = json.getString(DATE);
                    String TempMAXC = json.getString(MAXTEMPC);
                    String TempMinC = jObj.getString(MINTEMPC);

                    JSONArray description = json.getJSONArray(W_DESC);

                    for (int j = 0; j < description.length(); j++) {
                        JSONObject jObjDesc = description.getJSONObject(j);
                        value = jObjDesc.getString(VAL);
                    }   


                    JSONArray image = json.getJSONArray(W_IMG);

                    for (int k = 0; k < image.length(); k++) {
                        JSONObject jObjImage = image.getJSONObject(k);
                        valueimg = jObjImage.getString(VAL_IMG);
                    }

                    hmap.put(DATE, date);
                    hmap.put(MAXTEMPC, TempMAXC);
                    hmap.put(MINTEMPC, TempMinC);
                    hmap.put(VAL, value);
                    hmap.put(VAL_IMG, valueimg);

                    array.add(hmap);

                    Log.d("ArrayList", array.toString());
                }

                list.setAdapter(new SimpleAdapter(WeatherHome.this, array,
                        R.layout.custom_layout, new String[] { DATE, MAXTEMPC,
                                MINTEMPC, VAL, VAL_IMG }, new int[] {
                                R.id.tvDate, R.id.tvMaxTemp, R.id.tvMinTemp,
                                R.id.tvValue, R.id.weathercondition }));

            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }

        }

日志错误

03-05 19:25:01.780: E/AndroidRuntime(712): FATAL EXCEPTION: AsyncTask #1
03-05 19:25:01.780: E/AndroidRuntime(712): java.lang.RuntimeException: An error occured while executing doInBackground()
03-05 19:25:01.780: E/AndroidRuntime(712):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-05 19:25:01.780: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-05 19:25:01.780: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-05 19:25:01.780: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-05 19:25:01.780: E/AndroidRuntime(712):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-05 19:25:01.780: E/AndroidRuntime(712):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-05 19:25:01.780: E/AndroidRuntime(712):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-05 19:25:01.780: E/AndroidRuntime(712):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-05 19:25:01.780: E/AndroidRuntime(712):  at java.lang.Thread.run(Thread.java:856)
03-05 19:25:01.780: E/AndroidRuntime(712): Caused by: java.lang.NullPointerException
03-05 19:25:01.780: E/AndroidRuntime(712):  at com.ourcast.pocketweather.JSONReader.JSONRead(JSONReader.java:49)
03-05 19:25:01.780: E/AndroidRuntime(712):  at com.ourcast.pocketweather.WeatherHome$JSONParse.doInBackground(WeatherHome.java:91)
03-05 19:25:01.780: E/AndroidRuntime(712):  at com.ourcast.pocketweather.WeatherHome$JSONParse.doInBackground(WeatherHome.java:1)

注意: WeatherHome中的第91行是对JSONRead方法的调用,第1行显然是包语句。

2 个答案:

答案 0 :(得分:0)

JSONObject是一个非常敏感的json解析对象。如果句子中有任何无效字符,很容易失败。

在你的情况下,我认为由于“\ n”而解析jsonStr时会抛出JSONException。

删除“\ n”

替换以下行
jsonStr.append(line + "\n");

jsonStr.append(line);

答案 1 :(得分:0)

我添加了最低版本的Cupcake。我已将其更改为API 11,或者您也可以将其更改为8。此外,我已将WeatherHome.java中的@TargetApi(Build.VERSION_CODES.CUPCAKE)更改为@TargetApi(Build.New_Version)。它对我有用,我从JSON获取数据。