2016-03-30 18 views
2
'u kullanarak nasıl ayrıştırabilirim?

Kategori "Başlığı" na göre "Kategori", Nasıl Yapılır? "Fiyat" RecyclerView One Row'da "Fiyat" .json responce Çoklu Dizi Volley

[{ 
        "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 
      }]}] 

ve benim POJO Sınıflar gibi şunlardır::
benim Api url Bu URL gibi yanıt döndürür https://www.paidup.io/api/v1/businesses/212/menu

olduğunu

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

ve başka sınıfı: Şimdi

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

Bu aktivitemde'ü almak için ayrıştırmak istiyorum "kategorisinde" göre, recyclerview yılında
"başlığı" ve "fiyat" ihtiyacı ben bu yol gibi bunu yapmak için çalışılıyor am

: Ben Ayrıştırma tüm İstekleri çalıştı

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

ama başarı elde edemedi.

i peşin recyclerview için Öğeler

sayesinde Listesini almak için Try blokta yapmak için neye ihtiyacınız gerekir .... plz çözmek o

+0

plese efendim:

Eğer MenuRequest oluşan bir Array ile endup edecek öğeler öylesine
MenuRequest[] items = gson.fromJson(response.toString(), MenuRequest[].class); 

örneğin ne gerekiyorsa yapabileceğini sonra

nesneleri biri bana yardım edebilir mi – Dinesh

cevap

0

Sen tepki geri aramasında hata var.

1) JsonRequest voleybolunu kullanırsınız, ancak aslında bunu Gson ile POJO'ya ayırmak istiyorsunuz.

2) menurequest nesnelerin dizisi olsun ama sadece bir MenuRequest

olarak daha sonra ele görünüyor böylece gerçekten bu gerekmez:

JSONObject jsonObject = new JSONObject(); 

       String desc = jsonObject.getString("category"); 
       JsonParser jsonParser = new JsonParser(); 
       groupItem.items = (List<Item>) jsonParser.parse(String.valueOf(response)); 

Bunlar aslında farklı istisnalarını atar. yerine

Yapabileceğiniz:

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