Java 错误解析数据 org.json.JSONException: End of input at character 0 of of

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22691406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:19:02  来源:igfitidea点击:

Error parsing data org.json.JSONException: End of input at character 0 of

javaandroidjsonparsing

提问by ali

I'm trying to parse JSON data which is coming from this url.

我正在尝试解析来自这个 url 的JSON 数据。

But I am getting these errors:

但我收到这些错误:

03-27 16:48:21.019: E/Buffer Error(23717): Error converting result java.lang.NullPointerException

03-27 16:48:21.059: E/JSON Parser(23717): Error parsing data org.json.JSONException: End of input at character 0 of

03-27 16:48:21.019:E/缓冲区错误(23717):错误转换结果 java.lang.NullPointerException

03-27 16:48:21.059:E/JSON 解析器(23717):解析数据时出错 org.json.JSONException:在字符 0 处输入结束

Wwhen I debug my code; getJsonFromUrl()method returns null jobject. Here is the JSONParser classwhich I used. What's causing the error?

当我调试我的代码时;getJsonFromUrl()方法返回null jobject。这是我使用的JSONParser 类。导致错误的原因是什么?

public class JSONParser {

    static InputStream iStream = null;
    static JSONArray jarray = null;
    static JSONObject jObj= null;
    static String json = "";

    public JSONParser() {
    }



    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);



        try {
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            iStream.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parsing the string to a JSON object
        try {
            if (json != null) {
                jObj = new JSONObject(json);
            } else {
                jObj = null;
            }
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

I'm making the call for this method from another class using these lines. (url parameter is defined at top)

我正在使用这些行从另一个类调用此方法。(url 参数定义在顶部)

  JSONParser jParser = new JSONParser();
  final JSONObject jobject = jParser.getJSONFromUrl(url);

采纳答案by vzamanillo

You are trying to get the JSON content using a HTTP POST method instead of the appropiated GET method (W3schools.com GET vs.POST), modify your source code to simplify and fix your HTTP request

您正在尝试使用 HTTP POST 方法而不是适当的 GET 方法(W3schools.com GET vs.POST)获取 JSON 内容,请修改您的源代码以简化和修复您的 HTTP 请求

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);

try {
    HttpResponse httpResponse = httpClient.execute(get);
    String json = EntityUtils.toString(httpResponse.getEntity());
    System.out.println(json);
    ....
    ....

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