JSONException:在字符输入0结束 [英] JSONException: End of input at character 0
本文介绍了JSONException:在字符输入0结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从服务器获取JSON值。我不知道什么是错我的code。我使用的AsyncTask
这里是 doInBackground
code:
@覆盖
保护无效doInBackground(列表<弦乐> ... articlesRec){
对于(列表<串GT; articlesPostValue:articlesRec){
事件= articlesPostValue.get(0);
类别= articlesPostValue.get(1);
分页= articlesPostValue.get(2);
设备ID = articlesPostValue.get(3);
} 清单<&的NameValuePair GT; articlesPostValues =新的ArrayList<&的NameValuePair GT;();
articlesPostValues.add(新BasicNameValuePair(_事件,事件));
articlesPostValues.add(新BasicNameValuePair(猫,类别));
articlesPostValues.add(新BasicNameValuePair(分页,分页));
articlesPostValues.add(新BasicNameValuePair(_设备ID,设备ID)); HttpClient的HC =新DefaultHttpClient();
HttpPost马力=新HttpPost(AutoLifeConstants.BASE_URL); 尝试{
hp.setEntity(新UrlEn codedFormEntity(articlesPostValues)); //执行HTTP POST请求
HTT presponse响应= hc.execute(马力); 字符串结果= EntityUtils.toString(response.getEntity());
字符串jsontext =新的String(结果); JSONObject的条目=新的JSONObject(jsontext); this.response = entries.getString(response_ code); 尝试{
字符串totalPageStr = entries.getString(TOTAL_PAGES);
总页数= Integer.valueOf(totalPageStr);
}赶上(例外五){ } JSONArray postListArray = entries.getJSONArray(posts_list); 的for(int i = 0; I< postListArray.length();我++){
JSONObject的postListObj = postListArray.getJSONObject(I) 字符串articlesTitle = postListObj.getString(标题);
字符串日期时间= postListObj.getString(日期);
串articlesImage = postListObj.getString(feature_image);
字符串descrition = postListObj.getString(内容);
串fullDescription = postListObj.getString(full_content); articlesNewesttitle.add(articlesTitle);
articlesNewestPostTime.add(DATETIME);
articlesNewestPostImage.add(articlesImage);
articlesNewestPostDescription.add(descrition);
postFullDescription.add(fullDescription); Log.d(标题,articlesTitle);
} }赶上(例外五){
Log.e(逮住错误,Exceptin发生);
e.printStackTrace();
} 返回null;
}
这是赶上(例外五)
上的logcat
06-19 13:22:32.229:W / System.err的(19647):org.json.JSONException:输入结束在字符0
06-19 13:22:32.264:W / System.err的(19647):在org.json.JSONTokener.syntaxError(JSONTokener.java:450)
06-19 13:22:32.265:W / System.err的(19647):在org.json.JSONTokener.nextValue(JSONTokener.java:97)
06-19 13:22:32.268:W / System.err的(19647):在org.json.JSONObject<&初始化GT;(JSONObject.java:154)
06-19 13:22:32.269:W / System.err的(19647):在org.json.JSONObject<&初始化GT;(JSONObject.java:171)
06-19 13:22:32.270:W / System.err的(19647):在np.com.autolife.adapters.NewsActivity $ Get_postlist.doInBackground(NewsActivity.java:169)
06-19 13:22:32.272:W / System.err的(19647):在np.com.autolife.adapters.NewsActivity $ Get_postlist.doInBackground(NewsActivity.java:1)
06-19 13:22:32.272:W / System.err的(19647):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
06-19 13:22:32.273:W / System.err的(19647):在java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-19 13:22:32.273:W / System.err的(19647):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)
06-19 13:22:32.274:W / System.err的(19647):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-19 13:22:32.274:W / System.err的(19647):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573)
06-19 13:22:32.281:W / System.err的(19647):在java.lang.Thread.run(Thread.java:838)
下面就是 JSON 格式
{
搞定:[],
邮报:{
_event:get_posts,
_deviceID:490154203237518,
猫:2,
分页:1
},
response_ code:成功,
posts_total:8,
TOTAL_PAGES:2,
分页:1,
posts_list:[
{
ID:9168,
头衔:MAXXIS介绍Trepador轮胎
内容:,
full_content:HTTP:XXXXXX,
日期:2014年6月13日
类别:所有的新闻与活动,当地新闻
feature_image:HTTP:xxxxx.jpg,
feature_image_thumb:HTTP:xxxx.jpg,
作家:AutoLife队
},
{
ID:9162,
头衔:5年质保在所有英雄摩托车
内容:,
full_content:的http:// XXXXX,
日期:2014年6月13日
类别:所有的新闻与活动,当地新闻
feature_image:HTTP://xxxxxx.jpg,
feature_image_thumb:HTTP://xxxx.jpg,
作家:AutoLife队
},
{
ID:8130,
头衔:所有新的第11代丰田卡罗拉推出
内容:,
full_content:HTTP:// XXXXXX,
日期:2014年5月1日
类别:活动与放;方案,
feature_image:HTTP://xxxxxx.png,
feature_image_thumb:HTTP://xxxxxx.png,
作者:监督员
},
{
ID:9178,
头衔:新推出塔塔汽车公司:塔塔Nano扭曲,
内容:,
full_content:HTTP:// XXXXXX,
日期:2014年2月15日
类别:所有的新闻与活动,国际新闻
feature_image:HTTP://xxxxx.jpg,
feature_image_thumb:xxxxxxx.jpg,
作家:AutoLife队
},
{
ID:9175,
头衔:新推出塔塔汽车:Revotron引擎,
内容:,
full_content:XXXXXXX,
日期:2014年2月15日
类别:所有的新闻与活动,国际新闻
feature_image:HTTP://xxxxx.jpg,
feature_image_thumb:HTTP://xxxxx.jpg,
作家:AutoLife队
}
]
}
解决方案
您很可能得到一个空白的响应。它不为空,但在 jsontext 为空。所以,你得到这个错误,而不是一个空指针异常
您发送正确的参数server.Also检查URL响应POST请求与否。
i am trying to get JSON values from server. I don't know what is wrong with my code. I am using AsyncTask
and here is my code in doInBackground
Code:
@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;
}
And here is catch(Exception e)
on logcat
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)
Here is JSON format
{
"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"
}
]
}
解决方案
You are probably getting a blank response. Its not null but the jsontext is empty. So you are getting this error and not a Nullpointer exception
Are you sending right parameters to server.Also Check url respond to POST requests or not.
这篇关于JSONException:在字符输入0结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文