【问题标题】:Android JSON Exception (End of input at character 0 of)Android JSON 异常(字​​符 0 处的输入结束)
【发布时间】:2018-01-15 22:09:24
【问题描述】:

我正在尝试检索服务器 json 数据。

我的代码: 主活动

String url = "http://........com/...../...."
String[] params = new String[]{url};
JSONParser jsonParser = new JSONParser();
try {
String response = jsonParser.execute(url).get();
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObje = (JSONObject) jsonArray.get(0);
}

JSONParser.JAVA

HttpURLConnection connection = null;
BufferedReader reader = null;
String responseText="";
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            //GIVE ERROR THIS LINE!
            InputStream stream = connection.getInputStream();
            //ERROR LINE!

            reader = new BufferedReader(new InputStreamReader(stream));

错误:org.json.JSONException:输入结束于字符 0

Json 文件:

{"PersonelID":2,"PersonelAdıSoyadı":"New Driver","PersonelTelefon":"","PersonelMail":"driver@mail.com","PersonelPassword":null,"PersonelTipi":1,"AracID":0,"SirketID":20,"SuccessCode":1}

【问题讨论】:

  • 如果变量 "response" 的值包含此字符串 "{"PersonelID":2,"PersonelAdıSoyadı":"New Driver",..." 您会收到错误,因为这不是JSON 数组!不要使用JSONArray,而是使用JSONObject
  • 能否向我们展示完整的例外情况?

标签: android json parsing


【解决方案1】:

只需通过输入流调用此方法

    private String readInputStream(InputStream inputStream) throws IOException {
    Log.d(TAG, "readInputStream()");
    StringBuilder stringBuilder = new StringBuilder();

    BufferedReader bufferedReader = null;

    try {
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            bufferedReader = new BufferedReader(inputStreamReader);
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuilder.append(line);
                line = bufferedReader.readLine();

            }

            return stringBuilder.toString();

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        if (bufferedReader != null) {
            bufferedReader.close();
        }

    }

    return "RESULT_EMPTY";

}

,然后JSONObject jsonObject = new JSONObject(result);

【讨论】:

    【解决方案2】:

    错误的原因是因为您期望 JSONArray 采用这种形式

    ["item1","item2,"item3"]
    

    JSONObject 是这种形式{"key": "value"} 所以在你的例子中,你得到的是JSONObject,而不是JSONArray。所以把你的代码改成

    String response = jsonParser.execute(url).get();
    
    JSONObject jsonObject = new JSONObject(resonse);
    String PersonelMail= jsonObject.getString("PersonelMail");
     //...... the same for  the other values
    

    有关 JSON 解析的更多信息,请参阅tutorial。还可以查看我的answer,了解如何使用 Google 创建的库 Gson 轻松映射 JSON。

    【讨论】:

      【解决方案3】:

      我的代码是真的。问题是非常不同的地方。

      我正在向 Android 清单添加互联网权限,问题已解决。

      谢谢大家。

      【讨论】:

        猜你喜欢
        • 2012-01-02
        • 2016-10-04
        • 1970-01-01
        • 1970-01-01
        • 2017-05-17
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 1970-01-01
        相关资源
        最近更新 更多