Android org.json.JSONException:注释没有值
我有一个从wordpress博客上获取的Json数据,当我试图传递url时,我在我的日志中得到了一个错误,说org.Json.JSONException:No value for comments. 我想做的是从wordpress博客中检索信息 我已经粘贴了我的完整代码以供进一步参考Android org.json.JSONException:注释没有值,android,json,wordpress,Android,Json,Wordpress,我有一个从wordpress博客上获取的Json数据,当我试图传递url时,我在我的日志中得到了一个错误,说org.Json.JSONException:No value for comments. 我想做的是从wordpress博客中检索信息 我已经粘贴了我的完整代码以供进一步参考 03-13 11:43:59.698: W/System.err(18926): org.json.JSONException: No value for comments 03-13 11:43:59.698:
03-13 11:43:59.698: W/System.err(18926): org.json.JSONException: No value for comments
03-13 11:43:59.698: W/System.err(18926): at org.json.JSONObject.get(JSONObject.java:354)
03-13 11:43:59.698: W/System.err(18926): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
03-13 11:43:59.698: W/System.err(18926): at com.cepfmobileapp.org.CommentActivityWp$GetQuery.doInBackground(CommentActivityWp.java:271)
03-13 11:43:59.698: W/System.err(18926): at com.cepfmobileapp.org.CommentActivityWp$GetQuery.doInBackground(CommentActivityWp.java:1)
03-13 11:43:59.698: W/System.err(18926): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-13 11:43:59.698: W/System.err(18926): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 11:43:59.698: W/System.err(18926): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 11:43:59.708: W/System.err(18926): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-13 11:43:59.708: W/System.err(18926): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 11:43:59.708: W/System.err(18926): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 11:43:59.708: W/System.err(18926): at java.lang.Thread.run(Thread.java:856)
Java代码
private static final String TAG_COMMENTS = "comments";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_CONTENT = "content";
Intent i = getIntent();
// Receiving the Data
ida = i.getStringExtra("id");
url = "" + ida;
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
comments = jsonObj.getJSONArray(TAG_COMMENTS);
// looping through All Contacts
for (int i = 0; i < comments.length(); i++) {
JSONObject c = comments.getJSONObject(i);
// Phone node is JSON Object
//JSONObject comments = c.getJSONObject(TAG_COMMENTS );
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String content = c.getString(TAG_CONTENT);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_CONTENT, content);
// adding contact to contact list
queryList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
private static final String TAG_COMMENTS=“COMMENTS”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记_CONTENT=“CONTENT”;
Intent i=getIntent();
//接收数据
ida=i.getStringExtra(“id”);
url=”“+ida;
@凌驾
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
comments=jsonObj.getJSONArray(TAG_comments);
//通过所有触点循环
for(int i=0;ivalue
联系人。放置(标签号,ID);
联系人:put(标签名称、姓名);
联系人。放置(标签内容,内容);
//将联系人添加到联系人列表
queryList.add(联系人);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回null;
}
tythis
您的JSONArray中没有注释
,在您的回复中,您必须检查jsonObj.has(TAG\u CONTENT)
标记名是否存在
JSONObject jsonpost= jsonObj.getJSONObject("post");
if(jsonpost.has(TAG_ID)){
comments = jsonpost.getJSONArray(TAG_COMMENTS);
for (int i = 0; i < comments.length(); i++) {
String id = "";
String name = "";
String content = "";
if(c.has(TAG_ID))
id = c.getString(TAG_ID);
if(c.has(TAG_NAME))
name = c.getString(TAG_NAME);
if(c.has(TAG_CONTENT))
content = c.getString(TAG_CONTENT);
}
}
JSONObject jsonpost=jsonObj.getJSONObject(“post”);
if(jsonpost.has(TAG_ID)){
comments=jsonpost.getJSONArray(TAG_comments);
对于(inti=0;i
tythis
您的JSONArray中没有注释
,在您的回复中,您必须检查jsonObj.has(TAG\u CONTENT)
标记名是否存在
JSONObject jsonpost= jsonObj.getJSONObject("post");
if(jsonpost.has(TAG_ID)){
comments = jsonpost.getJSONArray(TAG_COMMENTS);
for (int i = 0; i < comments.length(); i++) {
String id = "";
String name = "";
String content = "";
if(c.has(TAG_ID))
id = c.getString(TAG_ID);
if(c.has(TAG_NAME))
name = c.getString(TAG_NAME);
if(c.has(TAG_CONTENT))
content = c.getString(TAG_CONTENT);
}
}
JSONObject jsonpost=jsonObj.getJSONObject(“post”);
if(jsonpost.has(TAG_ID)){
comments=jsonpost.getJSONArray(TAG_comments);
对于(inti=0;i
按如下方式进行尝试:
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
comments = jsonObj.getJSONArray("posts");
// looping through All Contacts
for (int i = 0; i < comments.length(); i++) {
JSONObject c = comments.getJSONObject(i);
//get the comments arraylist
JSONArray commentsdata = c.getJSONArray(TAG_COMMENTS);
for (int j = 0; j < commentsdata.length(); j++)
{
JSONObject comntObj = commentsdata.getJSONObject(j);
String name = comntObj.getString("name");
String sUrl = comntObj.getString("url");
String scontent = comntObj.getString("content");
....................
}
String id = c.getString(TAG_ID);
String content = c.getString(TAG_CONTENT);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_CONTENT, content);
// adding contact to contact list
queryList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
comments=jsonObj.getJSONArray(“posts”);
//通过所有触点循环
for(int i=0;ivalue
联系人。放置(标签号,ID);
联系人:put(标签名称、姓名);
联系人。放置(标签内容,内容);
//将联系人添加到联系人列表
queryList.add(联系人);
}
}捕获(JSONException e){
e、 printStackTrace();
}
按如下方式进行尝试:
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
comments = jsonObj.getJSONArray("posts");
// looping through All Contacts
for (int i = 0; i < comments.length(); i++) {
JSONObject c = comments.getJSONObject(i);
//get the comments arraylist
JSONArray commentsdata = c.getJSONArray(TAG_COMMENTS);
for (int j = 0; j < commentsdata.length(); j++)
{
JSONObject comntObj = commentsdata.getJSONObject(j);
String name = comntObj.getString("name");
String sUrl = comntObj.getString("url");
String scontent = comntObj.getString("content");
....................
}
String id = c.getString(TAG_ID);
String content = c.getString(TAG_CONTENT);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_CONTENT, content);
// adding contact to contact list
queryList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
comments=jsonObj.getJSONArray(“posts”);
//通过所有触点循环
for(int i=0;i