2016-03-30 49 views
2

如何在RecyclerView One Row中根據類別「標題」&「價格」 顯示「類別」。
我的API網址是https://www.paidup.io/api/v1/businesses/212/menujson responce is Multiple Array如何使用Volley解析它

此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

感謝.... PLZ解決這個問題

+0

普萊舍先生誰能幫我 – Dinesh

回答

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數組endup項目對象

,那麼你可以做任何你需要的,例如:

for(MenuRequest item:items) { 
          Log.d("test", "item: " +item); 
         }