JSONException:字符 0 处的输入结束

Posted

技术标签:

【中文标题】JSONException:字符 0 处的输入结束【英文标题】:JSONException: End of input at character 0 【发布时间】:2014-08-09 16:36:05 【问题描述】:

我正在尝试从服务器获取 JSON 值。我不知道我的代码有什么问题。我正在使用AsyncTask,这是我在doInBackground中的代码

代码:

@Override
    protected Void doInBackground(List<String>... articlesRec) 
        for (List<String> articlesPostValue : articlesRec) 
            event = articlesPostValue.get(0);
            category = articlesPostValue.get(1);
            paged = articlesPostValue.get(2);
            deviceID = articlesPostValue.get(3);
        

        List<NameValuePair> articlesPostValues = new ArrayList<NameValuePair>();
        articlesPostValues.add(new BasicNameValuePair("_event", event));
        articlesPostValues.add(new BasicNameValuePair("cat", category));
        articlesPostValues.add(new BasicNameValuePair("paged", paged));
        articlesPostValues.add(new BasicNameValuePair("_deviceID", deviceID));

        HttpClient hc = new DefaultHttpClient();
        HttpPost hp = new HttpPost(AutoLifeConstants.BASE_URL);

        try 
            hp.setEntity(new UrlEncodedFormEntity(articlesPostValues));

            // Execute HTTP Post Request
            HttpResponse response = hc.execute(hp);

            String result = EntityUtils.toString(response.getEntity());
            String jsontext = new String(result);

            JSONObject entries = new JSONObject(jsontext);

            this.response = entries.getString("response_code");

            try 
                String totalPageStr = entries.getString("total_pages");
                totalPage = Integer.valueOf(totalPageStr);
             catch (Exception e) 

            

            JSONArray postListArray = entries.getJSONArray("posts_list");

            for (int i = 0; i < postListArray.length(); i++) 
                JSONObject postListObj = postListArray.getJSONObject(i);

                String articlesTitle = postListObj.getString("title");
                String dateTime = postListObj.getString("date");
                String articlesImage = postListObj.getString("feature_image");
                String descrition = postListObj.getString("content");
                String fullDescription = postListObj.getString("full_content");

                articlesNewesttitle.add(articlesTitle);
                articlesNewestPostTime.add(dateTime);
                articlesNewestPostImage.add(articlesImage);
                articlesNewestPostDescription.add(descrition);
                postFullDescription.add(fullDescription);

                Log.d("Title",articlesTitle);
            

         catch (Exception e) 
            Log.e("catched error","Exceptin occured");
            e.printStackTrace();
        

        return null;
    

这里是 logcat 上的catch(Exception e)

    06-19 13:22:32.229: W/System.err(19647): org.json.JSONException: End of input at character 0 of 
06-19 13:22:32.264: W/System.err(19647):    at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
06-19 13:22:32.265: W/System.err(19647):    at org.json.JSONTokener.nextValue(JSONTokener.java:97)
06-19 13:22:32.268: W/System.err(19647):    at org.json.JSONObject.<init>(JSONObject.java:154)
06-19 13:22:32.269: W/System.err(19647):    at org.json.JSONObject.<init>(JSONObject.java:171)
06-19 13:22:32.270: W/System.err(19647):    at np.com.autolife.adapters.NewsActivity$Get_postlist.doInBackground(NewsActivity.java:169)
06-19 13:22:32.272: W/System.err(19647):    at np.com.autolife.adapters.NewsActivity$Get_postlist.doInBackground(NewsActivity.java:1)
06-19 13:22:32.272: W/System.err(19647):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-19 13:22:32.273: W/System.err(19647):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-19 13:22:32.273: W/System.err(19647):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-19 13:22:32.274: W/System.err(19647):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-19 13:22:32.274: W/System.err(19647):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-19 13:22:32.281: W/System.err(19647):    at java.lang.Thread.run(Thread.java:838)

这里是JSON格式

    
    "get": [],
    "post": 
        "_event": "get_posts",
        "_deviceID": "490154203237518",
        "cat": "2",
        "paged": "1"
    ,
    "response_code": "success",
    "posts_total": 8,
    "total_pages": 2,
    "paged": 1,
    "posts_list": [
        
            "ID": 9168,
            "title": "MAXXIS Introduce Trepador Tires",
            "content": "",
            "full_content": "http:xxxxxx",
            "date": "June 13, 2014",
            "category": "All News & Events, Local News",
            "feature_image": "http:xxxxx.jpg",
            "feature_image_thumb": "http:xxxx.jpg",
            "author": "AutoLife Team"
        ,
        
            "ID": 9162,
            "title": "5 Year Warranty On All Hero Motorcycles",
            "content": "",
            "full_content": "http://xxxxx",
            "date": "June 13, 2014",
            "category": "All News & Events, Local News",
            "feature_image": "http://xxxxxx.jpg",
            "feature_image_thumb": "http://xxxx.jpg",
            "author": "AutoLife Team"
        ,
        
            "ID": 8130,
            "title": "All new 11th Generation Toyota Corolla Launched",
            "content": "",
            "full_content": "http://xxxxxx",
            "date": "May 1, 2014",
            "category": "Events & Schemes",
            "feature_image": "http://xxxxxx.png",
            "feature_image_thumb": "http://xxxxxx.png",
            "author": "supervisor"
        ,
        
            "ID": 9178,
            "title": "New Launches From TATA Motors : TATA Nano Twist",
            "content": "",
            "full_content": "http://xxxxxx",
            "date": "February 15, 2014",
            "category": "All News & Events, International News",
            "feature_image": "http://xxxxx.jpg",
            "feature_image_thumb": "xxxxxxx.jpg",
            "author": "AutoLife Team"
        ,
        
            "ID": 9175,
            "title": "New Launches From TATA Motors : Revotron Engine",
            "content": "",
            "full_content": "xxxxxxx",
            "date": "February 15, 2014",
            "category": "All News & Events, International News",
            "feature_image": "http://xxxxx.jpg",
            "feature_image_thumb": "http://xxxxx.jpg",
            "author": "AutoLife Team"
        
    ]

【问题讨论】:

我已经更新了添加 JSON 的问题 第 169 行是什么? 这不是问题,但你为什么要这样做:String jsontext = new String(result); @user3751280 检查 jsontext 是否有一些值。 @GiruBhai 第 169 行是 ' JSONObject 条目 = new JSONObject(jsontext); ' . 【参考方案1】:

您收到此错误的原因是您尝试多次访问响应。例如,假设您对响应进行日志记录,然后使用相同的响应对象创建一个 json 对象,它会触发一次 acees 错误。

【讨论】:

【参考方案2】:

您可能会收到空白回复。它不为空,但 jsontext 为空。所以你得到这个错误而不是 Nullpointer 异常

您是否向服务器发送了正确的参数。还要检查 url 是否响应 POST 请求。

【讨论】:

这将是令人难以置信的,因为 OP 正在发布人们认为的响应。 @user3751280 使用 GET 实现它 @GiruBhai 我有同样的问题..我正在发送正确的参数但收到错误【参考方案3】:

在处理 JSON 之前检查响应是否为空:

if (response.success()) 
    if (response.getData() == null) 
        return null;
     else if (response.getData().length() <= 0)
        return null;
    

    try 
        // Logic

【讨论】:

以上是关于JSONException:字符 0 处的输入结束的主要内容,如果未能解决你的问题,请参考以下文章

JSONException 故障排除:在字符 0 Android 处输入结束

Volley JSONException:输入在字符 0 处结束

java.net.UnknownHostException:无法解析主机“<url>”:没有与主机名关联的地址和字符 0 处的输入结束

在 BigQuery 中读取时,未定义行 XXXX、列 xx-xx 处的 JSON 输入意外结束

SyntaxError:JSON.parse 处的 JSON 输入意外结束 [重复]

未捕获的 SyntaxError:JSON.parse (<anonymous>) 处的 JSON 输入意外结束