[英]How can I parse a JSON request using Volley?
I am using the Volley Facebook library to parse the JSON object by passing the String Parameter. 我正在使用Volley Facebook库通过传递String参数来解析JSON对象。 But it's throwing JSON exception.
但这会引发JSON异常。
[
{
"error":false,
"newsletter":[
{
"title":"IPS Informa",
"date":"2015-12-02",
"posted_by":"admin",
"image":"1449324052220174144.png",
"description":"Hello World",
"id":"4",
"post_count":"0"
},
{
"title":"IPS Informa",
"date":"2015-11-30",
"posted_by":"admin",
"image":"1449324052220174144.png",
"description":"Hello Worl Two",
"id":"1",
"post_count":"6"
}
]
}
]
And here is My Android Code to parse my JSON request: 这是我的Android代码,用于解析我的JSON请求:
StringRequest strReq = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hidePDialog();
try {
JSONObject first = new JSONObject(response);
String err = first.getString("error");
JSONArray second = first.getJSONArray("newsletter");
fisco_tips.clear();
// Parsing json
for (int i = 0; i < second.length(); i++) {
JSONObject obj = second.getJSONObject(i);
FisoTipsSinglton fisco_obj = new FisoTipsSinglton();
//Set Newsletter ID
fisco_obj.setFisco_id(obj.getString("id"));
//Set Title
fisco_obj.setTitle(obj.getString("title"));
//Set Posted Date
fisco_obj.setPosted_date(obj.getString("date"));
//Set Posted By
fisco_obj.setPosted_by(obj.getString("posted_by"));
//Set Image URL
fisco_obj.setThumbnailUrl(obj.getString("image"));
//Set Short Description
fisco_obj.setShort_description(obj.getString("description"));
fisco_obj.setPost_count(obj.getString("post_count"));
fisco_tips.add(fisco_obj);
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("Json Error", "Here is error: " + e.toString());
}
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hidePDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("country", country);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, related_posts);
}
How can I read my JSON object using string request in Android Volley library? 如何在Android Volley库中使用字符串请求读取JSON对象?
The error message that you're getting: 您收到的错误消息:
type org.json.JSONArray cannot be converted to JSONObject
org.json.JSONArray类型无法转换为JSONObject
indicates that you're trying to convert a JSONArray
to a JSONObject
. 表示您正在尝试将
JSONArray
转换为JSONObject
。 This happens in the first line of code in your try
block. 这发生在
try
块的第一行代码中。
JSONObject first = new JSONObject(response);
You're passing the entire response
to the JSONObject
constructor, but the response is wrapped in [ ]
, so it's a JSONArray
, not a JSONObject
. 您将整个
response
传递给JSONObject
构造函数,但是响应包装在[ ]
,因此它是JSONArray
而不是JSONObject
。 Your first step should be to parse the response
as a JSONArray
, get the JSONObject
from the first element of the array, then continue parsing. 您的第一步应该是将
response
解析为JSONArray
,从数组的第一个元素获取JSONObject
,然后继续解析。
try {
JSONArray wrapper = new JSONArray(response);
JSONObject first = (JSONObject)wrapper.get(0);
boolean err = first.getBoolean("error");
JSONArray newsletters = first.getJSONArray("newsletter");
// Parsing json
for (int i = 0; i < newsletters.length(); i++) {
JSONObject news = newsletters.getJSONObject(i);
.
.
.
}
} catch (JSONException e) {
Log.d("MainActivity.java", e.toString());
}
If there's a chance that your data source might return an empty array, you should do more error checking than I've shown here. 如果您的数据源有可能返回空数组,则应该执行比我在此处显示的更多的错误检查。
String err = first.getString("error");
Should be: 应该:
Boolean err = first.getBoolean("error");
Plus what Bill the Lizard said. 再加上比尔蜥蜴所说的话。
Instead of using Volley check OkHTTP with Retrofit. 而不是使用Volley,请选择OkHTTP with Retrofit。 It will wrap responses automatically ;)
它将自动包装响应;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.