【问题标题】:Android JSON WebRequest - JSONException: End of input at character 0Android JSON WebRequest - JSONException:字符 0 处的输入结束
【发布时间】:2016-12-12 09:46:25
【问题描述】:

我在这里有这段代码,我从http://mobilesiri.com/json-parsing-in-android-using-android-studio/ 获取并修改它以供我自己使用。

    public String makeWebServiceCall(String addr, int requestMethod) {
    URL url;
    String response = "";
    try {
        url = new URL(addr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15001);
        conn.setConnectTimeout(15001);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        if (requestMethod == Constant.GET) {
            conn.setRequestMethod("GET");
        } else if (requestMethod == Constant.DELETE) {
            conn.setRequestMethod("DELETE");
        }

        int reqResponseCode = conn.getResponseCode();

        if ((requestMethod == Constant.GET || requestMethod == Constant.DELETE) && reqResponseCode == HttpURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
        } else {
            response = "";
        }

    } catch (MalformedURLException murle) {
        Log.e(MalformedURLException.class.getName(), murle.getMessage());
    } catch (IOException ioe) {
        Log.e(IOException.class.getName(), ioe.getMessage());
    }

    return response;
}

把它和库放在一个普通的Java环境中,我可以得到这种形式的输出

{"status":"FOUND","data":[{"category_id":3,"category_name":"Experience Sharing Area"},{"category_id":4,"category_name":"Frequently Asked Questions"},{"category_id":1,"category_name":"General Pain Advice"},{"category_id":2,"category_name":"Pain Categorization Section"}]}

把这个放到Android中解析数据,我得到了JSONException - org.json.JSONException:

字符 0 处的输入结束

这是我实现它的地方。

protected Void doInBackground(Void... voids) {
    WebRequest webReq = new WebRequest();
    String jsonStr = webReq.makeWebServiceCall(Constant.WEB_SERVER_ADDR + Constant.GET_CATEGORY, Constant.GET);
    Log.d("URL:", Constant.WEB_SERVER_ADDR + Constant.GET_CATEGORY);
    Log.d("Response:", " > " + jsonStr);

    categoryList = parseJSON(jsonStr);

    return null;
}

这有什么问题吗?

【问题讨论】:

  • 发布 Logcat 和 parseJSON 类?

标签: java android json jsonexception


【解决方案1】:

您收到 JSONException: End of input at character 0

发布您的 parseJSON 类。

{"status":"FOUND","data":[{"category_id":3,"category_name":"经验 分享区"},{"category_id":4,"category_name":"常见问题 问题"},{"category_id":1,"category_name":"一般疼痛 忠告"},{"category_id":2,"category_name":"疼痛分类 部分"}]}

你的解析将是

try {
JSONObject reader = new JSONObject("YOUR_JSON_STRING");
JSONArray jsonArray = reader.getJSONArray("data");


for (int i = 0; i < jsonArray.length(); i++)
{
    JSONObject jsonItem = jsonArray.getJSONObject(i);

    try {
        jsonItem = jsonArray.getJSONObject(i);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }


    String cate_id= jsonItem.getString("category_id");
    String category_name= jsonItem.getString("category_name");

}
// Add adapter

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

}

【讨论】:

  • 我并没有真正做一个 parseJSON 类,奇怪的是我可以通过将 HTTPUrlConnection 更改为 URLConnection 来获取数据,但是我的后端服务器要求我有不同的 HTTP 请求。我在服务器端做 Phalcon PHP REST
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
相关资源
最近更新 更多