Android读取json返回 - 选择计数结果 - 导致错误

时间:2016-05-07 05:02:38

标签: php android json count

我在基于php结果的android上遇到了问题。
我试图接收count的SQL查询结果,结果被推出为 JSON数据,如下面的结果。

[{"count":"1"}]

然后这个结果将通过AsyncTask进入android。我希望计数结果应该成功接收,然后显示在列表视图上 但是,它似乎返回null而不是在下面的代码中获取计数。

 private class GetJsonLike extends AsyncTask<Integer, Void, String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Integer... params) {
        String uri = JSON_URL4 + params[0];
        Log.d("GET JSON", uri);
        BufferedReader bufferedReader = null;
        try {
            URL url = new URL(uri);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            StringBuilder sb = new StringBuilder();

            bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String json;
            while ((json = bufferedReader.readLine()) != null) {
                sb.append(json + "\n");
            }
            return sb.toString().trim();
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    protected void onPostExecute(String s) {
        List<Like> dbData = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(s);

            for (int n = 0; n < array.length(); n++) {
                JSONObject jObject = array.getJSONObject(n);
                Like like = new Like(jObject.getLong("count"));
                dbData.add(like);


                likeSt[1] = likeSt[1].concat("Username:" + like.getLikeNum() + "\b\b" + "Time:");

                ArrayList<String> likeList = new ArrayList<String>();
                likeList.addAll(Arrays.asList(likeSt));
                lv_likeAdapter = new ArrayAdapter<String>(DetailActivity.this, R.layout.list1, likeList);
                lv_like.setAdapter(lv_likeAdapter);

            }

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


}

我认为这是由于StringBuilder sb返回null并导致崩溃 但我不知道为什么它返回null并且没有得到数据 因为,这段代码可以成功获取其他php数据,例如以下 JSON数据,这不是计数结果类型。

  

[{“userName”:“Zero”,“cmDetail”:“test002”,“cmTime”:“2016-05-06 19:41:56”},{“userName”:“Zero”,“cmDetail “:”test001“,”cmTime“:”2016-05-06 19:41:13“},{”userName“:”Zero“,”cmDetail“:”1234“,”cmTime“:”2016-05- 06 19:40:25“}]

寻求建议以应对这个计数问题!谢谢!

PS:我声明包含计数结果的变量是long,之前尝试过int,但似乎也没有用。

其他信息Logcat thx提醒我:D

05-07 14:20:36.592 30508-30508/? E/AndroidRuntime: FATAL EXCEPTION: main
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime: Process: bhouse.travellist, PID: 30508
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at org.json.JSONArray.<init>(JSONArray.java:92)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at org.json.JSONArray.<init>(JSONArray.java:108)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at bhouse.travellist.DetailActivity$GetJsonLike.onPostExecute(DetailActivity.java:540)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at bhouse.travellist.DetailActivity$GetJsonLike.onPostExecute(DetailActivity.java:505)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at android.os.AsyncTask.finish(AsyncTask.java:651)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at android.os.AsyncTask.access$500(AsyncTask.java:180)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:224)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5514)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
05-07 14:20:36.592 30508-30508/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

0 个答案:

没有答案