如何从Json使用Volley

时间:2018-05-16 13:31:41

标签: java android json

我按照以下链接

但它对我无能为力。

在这个链接中,我将使用@Kuldip Kulkerni回答,我将获得数据,但它是以DESC顺序进行的。

如何在我的Recyclerview中安排主要类别和子类别并显示

我试过看下面的代码:

final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                url, jsonObject, new Response.Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());

                try {

                    JSONArray jArray = response.getJSONArray("data");

                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);

                        Pojo dataPojo = new Pojo();
                        dataPojo.id = json_data.getString("category_id");
                        dataPojo.fname = json_data.getString("category_name");
                        dataPojo.thumbnailUrl = json_data.getString("category_image");
                        dataList.add(i, dataPojo);

                        JSONArray jArray1 = json_data.getJSONArray("subcategory");

                        for (int j = 0; j < jArray1.length(); j++) {
                            JSONObject json_data1 = jArray1.getJSONObject(j);

                            Pojo dataPojo1 = new Pojo();
                            dataPojo1.id2 = json_data1.getString("category_id");
                            dataPojo1.fname2 = json_data1.getString("category_name");
                            dataList.add(j, dataPojo1);
                        }

                    }

                    dataAdapter = new DataAdapter(dataList);
                    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
                    recyclerView.setLayoutManager(mLayoutManager);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(dataAdapter);
                    dataAdapter.notifyDataSetChanged();

                }
  • 上面的代码是获取主类别的数据,但无法获取子类别数据

以下是我的Pojo.java课程:

public class Pojo {

    public String id;
    public String fname;
    public String thumbnailUrl;
    public String id2;
    public String fname2;

    public Pojo() {
    }

    public Pojo(String id, String fname, String thumbnailUrl, String id2, String fname2) {
        this.id = id;
        this.fname = fname;
        this.thumbnailUrl = thumbnailUrl;
        this.id2 = id2;
        this.fname2 = fname2;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getThumbnailUrl() {
        return thumbnailUrl;
    }

    public void setThumbnailUrl(String thumbnailUrl) {
        this.thumbnailUrl = thumbnailUrl;
    }

    public String getId2() {
        return id2;
    }

    public void setId2(String id2) {
        this.id2 = id2;
    }

    public String getFname2() {
        return fname2;
    }

    public void setFname2(String fname2) {
        this.fname2 = fname2;
    }

}

Json 回复:

enter image description here

所以如何解决这个问题可以帮到任何人

@Birju ANSWER后的EIDT问题

enter image description here

提前致谢。 :)

1 个答案:

答案 0 :(得分:2)

更改Pojo课程

public class Pojo {

private String catagory_id;
private String catagory_name;
private String catagory_thumbnailUrl;
private ArrayList<SubCatagory> subCatagoryArrayList;

public ArrayList<SubCatagory> getSubCatagoryArrayList() {
    return subCatagoryArrayList;
}

public void setSubCatagoryArrayList(ArrayList<SubCatagory> subCatagoryArrayList) {
    this.subCatagoryArrayList = subCatagoryArrayList;
}

public String getCatagory_id() {
    return catagory_id;
}

public void setCatagory_id(String catagory_id) {
    this.catagory_id = catagory_id;
}

public String getCatagory_name() {
    return catagory_name;
}

public void setCatagory_name(String catagory_name) {
    this.catagory_name = catagory_name;
}

public String getCatagory_thumbnailUrl() {
    return catagory_thumbnailUrl;
}

public void setCatagory_thumbnailUrl(String catagory_thumbnailUrl) {
    this.catagory_thumbnailUrl = catagory_thumbnailUrl;
}
}

将新类添加为SubCatagory

public class SubCatagory {
private String subcatagory_id;
private String subcatagory_name;


public String getSubcatagory_id() {
    return subcatagory_id;
}

public void setSubcatagory_id(String subcatagory_id) {
    this.subcatagory_id = subcatagory_id;
}

public String getSubcatagory_name() {
    return subcatagory_name;
}

public void setSubcatagory_name(String subcatagory_name) {
    this.subcatagory_name = subcatagory_name;
}
}

并在你的onResponce()

  try {

                JSONArray jArray = response.getJSONArray("data");

                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);

                    Pojo dataPojo = new Pojo(); //this is catagory class
                    dataPojo.setCatagory_id(json_data.getString("category_id"));
                    dataPojo.setCatagory_name(json_data.getString("category_name"));
                    dataPojo.setCatagory_thumbnailUrl(json_data.getString("category_image"));


                    JSONArray jArray1 = json_data.getJSONArray("subcategory");

                    ArrayList<SubCatagory> subCatagoryArrayList = new ArrayList<>();
                    for (int j = 0; j < jArray1.length(); j++) {
                        JSONObject json_data1 = jArray1.getJSONObject(j);

                        SubCatagory subCatagory = new SubCatagory(); // this is subcatagory class
                        subCatagory.setSubcatagory_id(json_data1.getString("category_id"));
                        subCatagory.setSubcatagory_name(json_data1.getString("category_name"));
                        subCatagoryArrayList.add(subCatagory);
                    }
                    dataPojo.setSubCatagoryArrayList(subCatagoryArrayList);//this i insert subcatagory in catagory 
                    dataList.add(dataPojo); // and after i add catagory to datalist 
                }

              /*  dataAdapter = new DataAdapter(dataList);
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
                recyclerView.setLayoutManager(mLayoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(dataAdapter);
                dataAdapter.notifyDataSetChanged();*/
  }

注意: - 1 评论recyclerviewdataadapter并在调试模式下检查responce vales,因为我使用getter setter对于变量(对于后续编码结构:不要使变量公开......

  • 2不要直接替换我的代码...首先评论您的旧代码

并在

  

dataadapter - &gt;在方法onbindviewholder(){

     

Pojo catagory = dataList.get(position); //这给你了catagory

     

Arraylist subcatagory =   catagory.getSubCatagoryArrayList(); ///这是子目录   arraylist ...使用for循环可以得到子目录数据

     

}