解析对象&;数组到ListView android
我花了4个小时为一个Android应用程序设置对象或数组来显示listview,不幸的是结果为空。 我想得到:解析对象&;数组到ListView android,android,json,android-volley,Android,Json,Android Volley,我花了4个小时为一个Android应用程序设置对象或数组来显示listview,不幸的是结果为空。 我想得到: 发布-->id、url、标题和内容,然后 类别-->id和标题,以及最后一个 附件-->图像-->完整-->url { "status": "ok", "count": 4, "count_total": 4, "pages": 1, "posts": [ { "id": 16,
- 发布-->id、url、标题和内容,然后
- 类别-->id和标题,以及最后一个
- 附件-->图像-->完整-->url
{
"status": "ok",
"count": 4,
"count_total": 4,
"pages": 1,
"posts": [
{
"id": 16,
"url": "url,
"status": "publish",
"title": "2014 Yamaha FZ1",
"title_plain": "2014 Yamaha FZ1",
"content": "",
"categories": [
{
"id": 1,
"slug": "sport-motorcycle",
"title": "Sport Motorcycle",
"description": "",
"parent": 0,
"post_count": 2
}
],
"author": {
"id": 1,
"first_name": "",
},
"comments": [],
"attachments": [
{
"id": 17,
"url": "image url",
"slug": "yamaha-fz1",
"title": "2014 Yamaha FZ1 ",
"description": "",
"caption": "",
"parent": 16,
"mime_type": "image/jpeg",
"images": {
"full": {
"url": "http://demo..jpg",
"width": 640,
"height": 426
},
"thumbnail": {
"url": "http://demo..jpg",
"width": 150,
"height": 150
},
"medium": {
"url": "http://demo..jpg",
"width": 300,
"height": 199
},
"large": {
"url": "http://demo..jpg",
"width": 640,
"height": 426
}
}
}
],
"comment_count": 0,
"comment_status": "open",
"thumbnail": "http://demo..jpg",
"custom_fields": {
"slide": [
"http://demo..jpg"
]
},
........
}
请帮忙,我很困惑:(提前谢谢
我的代码:
JsonArrayRequest postRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
PostModel pm = new PostModel();
JSONObject posts = obj.getJSONObject("posts");
pm.settitle(posts.getString("title"));
pm.settitlePlain(posts.getString("titleplain"));
JSONObject category = obj.getJSONObject("category");
postList.add(pm);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
JsonArrayRequest postRequest=新的JsonArrayRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(TAG,response.toString());
hidePDialog();
对于(int i=0;i
解决方案:
JSONArray posts = response.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
JSONObject obj = posts.getJSONObject(i);
PostModel pm = new PostModel();
pm.setJudul(obj.getString("title"));
pm.setIsi(obj.getString("content"));
JSONArray categories = obj.getJSONArray("categories");
for (int k = 0; k < categories.length(); k++) {
JSONObject obj1 = categories.getJSONObject(k);
pm.setCategory(obj1.getString("title"));
}
JSONObject thumbnail = obj.getJSONObject("thumbnail_images");
for (int j = 0; j < thumbnail.length(); j++) {
JSONObject medium = thumbnail.getJSONObject("medium");
pm.setThumbnail(medium.getString("url"));
}
postList.add(pm);
}
JSONArray posts=response.getJSONArray(“posts”);
对于(int i=0;i
我认为您应该使用
JsonObjectRequest
notJsonArrayRequest
,因为您接收的是对象而不是数组
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Method.GET,
url,空,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
我认为您不需要外部循环,因为它是一个JSONObject,请检查此项
JSONObject posts = obj.getJSONObject("posts");
到
JSONObject posts=obj.getJSONArray(“posts”);
对于(inti=0;i我建议您使用Gson
Gson是一个来自Google的库,它使用反射来解析JSON字符串,在像您这样简单的情况下,它的神奇之处在于!因此,您唯一的工作应该是使用JSON的结构创建模型对象
public class YourObject {
private String status;
private int count;
private int count_total;
private int pages;
private List<Post> posts;
}
public class Post {
private int id;
private String url;
private String title;
private String title_plain;
private String content;
private List<Category> categories;
//... and so on
}
仅此而已。您可以在这里找到Gson-->试试这种方法,希望这能帮助您解决问题。
以下是HashMap的ArrayList,而不是自定义模型类:
JSONArray postsJsonArray = obj.getJSONArray("posts");
ArrayList<HashMap<String,Object>> postList = new ArrayList<HashMap<String, Object>>();
for(int j=0;j<postsJsonArray.length();j++){
HashMap<String,Object> post = new HashMap<String, Object>();
post.put("id",postsJsonArray.getJSONObject(j).getString("id"));
post.put("url",postsJsonArray.getJSONObject(j).getString("url"));
post.put("title",postsJsonArray.getJSONObject(j).getString("title"));
post.put("content",postsJsonArray.getJSONObject(j).getString("content"));
ArrayList<HashMap<String,String>> categoryList = new ArrayList<HashMap<String, String>>();
JSONArray categoriesJsonArray = postsJsonArray.getJSONObject(j).getJSONArray("categories");
for(int k=0;k<categoriesJsonArray.length();k++) {
HashMap<String, String> category = new HashMap<String, String>();
category.put("id", categoriesJsonArray.getJSONObject(j).getString("id"));
category.put("title", categoriesJsonArray.getJSONObject(j).getString("title"));
categoryList.add(category);
}
ArrayList<HashMap<String,String>> attachmentList = new ArrayList<HashMap<String, String>>();
JSONArray attachmentJsonArray = postsJsonArray.getJSONObject(j).getJSONArray("categories");
for(int l=0;l<attachmentJsonArray.length();l++) {
HashMap<String, String> attachment = new HashMap<String, String>();
attachment.put("id", attachmentJsonArray.getJSONObject(j).getJSONObject("image").getJSONObject("full").getString("url"));
attachmentList.add(attachment);
}
post.put("attachment",attachmentList);
postList.add(post);
}
JSONArray postsjssonarray=obj.getJSONArray(“posts”);
ArrayList postList=新的ArrayList();
对于(int j=0;jJsonObjectRequest JsonObjectRequest=new JsonObjectRequest(Method.GET,
url,空,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject jsonobj){
试一试{
对于(int i=0;iDya,所以您在解析Json数据时遇到问题,对吗?@Dya,您的Json无效。首先检查您发布到服务器的变量是否包含正确的键值??其次,因为该Json将无效。要检查您的Json是否有效,请使用此链接@pratt:hi。很高兴再次见到您。我无法获取数据onRespose@Haresh:Json运行良好,我只发布了部分内容。但首先它的Json无效。这应该是注释或在此处添加部分代码!!我尝试使用JsonObjectRequest,只对第一个字符串为:JSONObject phone=response.getJSONObject(“posts”)的对象有效;接下来的结果是null@Dya你能提供它的URL吗???@Dya好的,没问题。只要删除你上面的评论!谢谢你的建议,但如果我切换到其他代码。一切都会很复杂,对吗?…我会在解决这个问题后再试。我会现在再试,等等..我会让你知道结果。:)谢谢你,我必须把代码放在哪里?如果没有自定义模型类?适配器设置为listview怎么样?如果我打扰了,很抱歉you@Dya,你的意思是不知道如何从ArrayList HashMap中访问数据?我只是理解你代码的规则。我不知道放在哪里?因为自定义是必需的。这是我研究中的一个要求。所以你需要使用模型对于每个JSONArray,您都需要创建模型类(也称为getter setter方法),对于它们,还需要创建每个arraylist。@Dya
JSONArray postsJsonArray = obj.getJSONArray("posts");
ArrayList<HashMap<String,Object>> postList = new ArrayList<HashMap<String, Object>>();
for(int j=0;j<postsJsonArray.length();j++){
HashMap<String,Object> post = new HashMap<String, Object>();
post.put("id",postsJsonArray.getJSONObject(j).getString("id"));
post.put("url",postsJsonArray.getJSONObject(j).getString("url"));
post.put("title",postsJsonArray.getJSONObject(j).getString("title"));
post.put("content",postsJsonArray.getJSONObject(j).getString("content"));
ArrayList<HashMap<String,String>> categoryList = new ArrayList<HashMap<String, String>>();
JSONArray categoriesJsonArray = postsJsonArray.getJSONObject(j).getJSONArray("categories");
for(int k=0;k<categoriesJsonArray.length();k++) {
HashMap<String, String> category = new HashMap<String, String>();
category.put("id", categoriesJsonArray.getJSONObject(j).getString("id"));
category.put("title", categoriesJsonArray.getJSONObject(j).getString("title"));
categoryList.add(category);
}
ArrayList<HashMap<String,String>> attachmentList = new ArrayList<HashMap<String, String>>();
JSONArray attachmentJsonArray = postsJsonArray.getJSONObject(j).getJSONArray("categories");
for(int l=0;l<attachmentJsonArray.length();l++) {
HashMap<String, String> attachment = new HashMap<String, String>();
attachment.put("id", attachmentJsonArray.getJSONObject(j).getJSONObject("image").getJSONObject("full").getString("url"));
attachmentList.add(attachment);
}
post.put("attachment",attachmentList);
postList.add(post);
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonobj) {
try {
for(int i=0;i<jsonobj.getJSONArray("posts").length();i++){
String id=jsonobj.getJSONArray("posts").getJSONObject(i).getString("id");
for(int j=0;j<jsonobj.getJSONArray("posts").getJSONObject(i).getJSONArray("categories").length();j++){
String title=jsonobj.getJSONArray("posts").getJSONObject(i).getJSONArray("categories").getJSONObject(j).getString("title");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});