我正在尝试从服务器获取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;
}
这是catch(Exception e)
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)
这是 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"
}
]
}
您可能会得到一个空白的答复。它不为null,但 jsontext 为空。因此,您将收到此错误,而不是Nullpointer异常
您是否正在向服务器发送正确的参数。还要检查url是否响应POST请求。
以下是所有的错误: 下面是我的代码:
问题内容: 我已经看到其他人遇到了这个问题,但是没有一个帖子能够帮助我。我正在尝试将Volley用于我的REST调用库,当我尝试将JSON对象作为参数使用Put调用时,我得到了。 这是代码: jsonParams日志显示: 我还有其他设置吗?看来请求无法解析JSON对象。我什至尝试创建一个HashMap,然后使用它创建一个JSON对象,但是我仍然得到相同的结果。 问题答案: 我也遇到了这个问题。
问题内容: 我在php中有一个API,可以JSON格式发送数据。我编写了以下代码,当我使用wifi时可以正常工作。但是,当我想在3g上从API下载数据时,会收到以下异常:JSONException:在字符0的输入结束 我不知道为什么它可以在wifi上工作,但不能在移动互联网上使用。我的代码: 有人可以帮助我吗? 问题答案: 您可能会得到一个空白的答复。它不为null,但响应为空。因此,您将收到此错
我见过其他人遇到这个问题,但没有一个帖子能够帮助我。我试图在REST调用库中使用Volley,当我试图使用带有JSON对象的Put调用作为参数时,我得到了错误:org。json。JSONException:输入结束于的字符0处。 代码如下: jsonParams日志显示: 有没有其他我错过的场景?看起来请求无法解析JSON对象。我甚至尝试创建一个HashMap,然后用它创建一个JSON对象,但仍然
我正在尝试登录应用程序,但我一直遇到“解析数据org.json.JSONException错误:在字符0的输入结束” 我的登录名。java看起来像这样 公共类登录扩展活动实现OnClickListener{私有EditText用户,pass; 私有静态最终字符串LOGIN_URL="my url"; //从真实服务器进行测试: } } 我的JSONParser。java如下所示:` 公共类JSON
问题内容: 我正在尝试从android解析json,但出现了这个奇怪的异常。我的json数据是 {“ id”:“ 1”,“ owner”:“ 1”,“ name”:“ gravitas”,“ description”:“是一场盛会”,“ start_time”:“ 0000-00-00 00:00:00 “,” end_time“:” 0000-00-00 00:00:00“,” venue“:”