android应用程序中的JSONException

时间:2014-09-18 10:45:08

标签: android

我收到此异常,但我不明白为什么,我的应用程序运行得非常好,直到今天,我不会在服务器的http请求中插入任何更改。< / p>

这是我的代码:

protected ArrayList<String> doInBackground(Void... params) {
        result = null;
        inputStream = null;
        ArrayList<String> person = new ArrayList<String>();
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url[0]);
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("userName", LoginFragment.getUserName()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
        } catch (Exception exception) {
            Log.e("retrieve persons", "Error in http connection " + exception.toString());
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 8192);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            inputStream.close();
            reader.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("Error", "Error converting result " + e.toString());
        }
        if (!result.contains("null")) {
            try {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject tuple = jArray.getJSONObject(i);
                    person.add(tuple.getString("name"));
                }
            } catch (JSONException e) {
                Log.e("Parser Problem", e.toString());
            }
        }
        return person;
    }

我的logcat:

 E/Parser Problem(7078): org.json.JSONException: End of input at character 0 of 

我再说一遍,这个功能一直工作到昨天,是否有可能出现服务器问题?

提前感谢。

1 个答案:

答案 0 :(得分:0)

首先在Logs中打印“result”并检查它是否是有效的JSON。 您可以使用http://jsonformatter.curiousconcept.com/进行检查。 只需复制网站上给出的编辑框中的结果字符串,然后单击验证。