如何根据类别“标题”显示“类别”,& “价钱”
在RecyclerView One Row中。
我的Api网址是https://www.paidup.io/api/v1/businesses/212/menu
此Url会返回如下响应:
[{
"category": "Espresso & Coffee",
"items": [{
"title": "Vacuum Coffee",
"size": [{
"label": "medium",
"price": 125
}, {
"label": "large",
"price": 145
}]
}, {
"title": "Cafe Latte",
"size": [{
"label": "small",
"price": 115
}, {
"label": "medium",
"price": 135
}, {
"label": "large",
"price": 150
}]
}, {
"title": "Cafe Mocha",
"size": [{
"label": "small",
"price": 115
}, {
"label": "medium",
"price": 135
}, {
"label": "large",
"price": 150
}]
}, {
"title": "White Mocha",
"size": [{
"label": "small",
"price": 120
}, {
"label": "medium",
"price": 145
}, {
"label": "large",
"price": 155
}]
}, {
"title": "Caramel Macchiato",
"size": [{
"label": "small",
"price": 125
}, {
"label": "medium",
"price": 145
}, {
"label": "large",
"price": 165
}]
}, {
"title": "Coffee De Leche",
"size": [{
"label": "small",
"price": 130
}, {
"label": "medium",
"price": 145
}, {
"label": "large",
"price": 160
}]
}, {
"title": "Cafe-UK Coffee Lava",
"size": null,
"price": 145
}]
}, {
"category": "Frappe",
"items": [{
"title": "Coffee Base",
"size": [{
"label": "small",
"price": 140
}, {
"label": "medium",
"price": 155
}, {
"label": "large",
"price": 170
}]
}, {
"title": "Milk Base",
"size": [{
"label": "small",
"price": 130
}, {
"label": "medium",
"price": 145
}, {
"label": "large",
"price": 160
}]
}, {
"title": "Cream Soda Float w\/Ice-cream",
"size": null,
"price": 140
}]
}, {
"category": "Milk Tea & Juice",
"items": [{
"title": "Milk Tea",
"size": [{
"label": "small",
"price": 105
}, {
"label": "medium",
"price": 120
}]
}, {
"title": "Hot Tea",
"size": null,
"price": 120
}, {
"title": "Italian Soda",
"size": [{
"label": "small",
"price": 105
}, {
"label": "medium",
"price": 120
}, {
"label": "large",
"price": 140
}]
}, {
"title": "Juice",
"size": null,
"price": 105
}]
}, {
"category": "Food",
"items": [{
"title": "Ultimate Chili Con Fries",
"size": null,
"price": 290
}, {
"title": "Nachos",
"size": null,
"price": 290
}, {
"title": "Marble Potato Fondue",
"size": null,
"price": 120
}, {
"title":"Breakfast",
"size":null,
"price":220
}]}]
和我的POJO类似:
package ph.paidup.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by oxiloindia on 3/30/2016.
*/
public class Item {
@SerializedName("title")
@Expose
private String title;
@SerializedName("size")
@Expose
private Object size;
@SerializedName("price")
@Expose
private Integer price;
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The size
*/
public Object getSize() {
return size;
}
/**
*
* @param size
* The size
*/
public void setSize(Object size) {
this.size = size;
}
/**
*
* @return
* The price
*/
public Integer getPrice() {
return price;
}
/**
*
* @param price
* The price
*/
public void setPrice(Integer price) {
this.price = price;
}
}
和另一个班级:
package ph.paidup.models;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by oxiloindia on 3/30/2016.
*/
@Generated("org.jsonschema2pojo")
public class MenuRequest {
@SerializedName("category")
@Expose
private String category;
@SerializedName("items")
@Expose
private List<Item> items = new ArrayList<Item>();
/**
*
* @return
* The category
*/
public String getCategory() {
return category;
}
/**
*
* @param category
* The category
*/
public void setCategory(String category) {
this.category = category;
}
/**
*
* @return
* The items
*/
public List<Item> getItems() {
return items;
}
/**
*
* @param items
* The items
*/
public void setItems(List<Item> items) {
this.items = items;
}
}
现在我想在我的活动中解析这个问题 根据 “类别”,需要其中 “标题”和 “recyclerview”中的“价格”
我正在尝试这样做:
public void getInVoice() {
showProgress(true);
String URL="https://www.paidup.io/api/v1/businesses/212/menu";
JsonArrayRequest req = new JsonArrayRequest(Request.Method.GET,URL,null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
showProgress(false);
VolleyLog.v("Response:%n %s", response);
Gson gson = new GsonBuilder().create();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = new JSONObject();
String desc = jsonObject.getString("category");
JsonParser jsonParser = new JsonParser();
groupItem.items = (List<Item>) jsonParser.parse(String.valueOf(response));
// Create a new adapter with data items
mExpandableAdapter = new BakeryMenuExpandableAdapter(getActivity(), setUpList(groupItem.items)) {
@Override
public void onItemClick(int position, View v) {
}
};
// Attach this activity to the Adapter as the ExpandCollapseListener
mExpandableAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
@Override
public void onListItemExpanded(int position) {
Log.e("CHEEE", "" + position);
}
@Override
public void onListItemCollapsed(int position) {
}
});
// Set the RecyclerView's adapter to the ExpandableAdapter we just created
recyclerView.setAdapter(mExpandableAdapter);
// Set the layout manager to a LinearLayout manager for vertical list
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
} catch (Exception e) {
Log.e("My App", "Could not parse malformed JSON: \"" + response + "\"");
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Activity activity = getActivity();
if (activity != null && isAdded())
showProgress(false);
Toast.makeText(getActivity(), "Something went wrong. please try it again", Toast.LENGTH_SHORT).show();
}
});
// add the request object to the queue to be executed
addToRequestQueue(req, TAG);
}
我尝试了所有请求解析但无法取得成功。
我需要在Try块中做什么来获取recyclerview的项目列表
提前感谢....请解决它
答案 0 :(得分:0)
您的回复回调中有错误。
1)你使用JsonRequest排球,但实际上你想用Gson将它解析为POJO。
2)你获得了一系列menurequest对象,但它似乎只处理了一个MenuRequest
所以你真的不需要那些:
JSONObject jsonObject = new JSONObject();
String desc = jsonObject.getString("category");
JsonParser jsonParser = new JsonParser();
groupItem.items = (List<Item>) jsonParser.parse(String.valueOf(response));
这些实际上引发了不同的例外。
你可以这样做:
MenuRequest[] items = gson.fromJson(response.toString(), MenuRequest[].class);
所以在项目中你会得到一个MenuRequest对象数组
然后你可以做任何你需要的事情,例如:
for(MenuRequest item:items) {
Log.d("test", "item: " +item);
}