2016-03-30 2 views
2

カテゴリー "タイトル"によると、カテゴリー "タイトル"によると、& "価格" RecyclerView One Row。json responceは複数の配列ですどのように私はそれを解析することができますボレー

[{ 
        "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クラスが好きです:
私のAPIのURLは、このURLのような応答を返すhttps://www.paidup.io/api/v1/businesses/212/menu

ある

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); 
} 

成功することはできませんでした。

iは、事前にrecyclerviewのためのアイテムの

おかげでリストを取得するために、tryブロックで行うには何が必要なはずです.... plzは解決それ

+0

pleseは先生誰でも助けてくれる? – Dinesh

答えて

0

あなたはあなたの応答のコールバックのエラーを持っています。

1)あなたはvolleyのJsonRequestを使用しますが、実際にはそれをGsonを使用してPOJOに解析します。

2)あなたはmenurequestオブジェクトの配列を取得しますが、あなただけの1 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); 
         } 
関連する問題