2016-03-30 7 views
2

Wie zeigt man "Kategorie", nach Kategorie "Titel", & "Preis" in RecyclerView One Row.
meine Api url ist https://www.paidup.io/api/v1/businesses/212/menuAntwort von json ist Multiple Array Wie kann ich es mit Hilfe von Volley

Diese URL gibt Antwort wie:

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

und meine POJO Klassen sind wie:

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

und eine andere Klasse:

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

Jetzt Ich möchte dies in meiner Aktivität zu Getanalysieren Nach „Kategorie“, muss seine
„Titel“ und „Preis“ in recyclerview

Ich mag es so zu tun versuche:

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

ich alle Anfragen versucht zu Parse es konnte aber keinen Erfolg haben.

Was soll ich in Try-Block tun müssen Liste der Gegenstände bekommen für recyclerview

Dank im Voraus .... plz lösen es

+0

plese sir Kann mir jemand helfen – Dinesh

Antwort

0

Sie haben Fehler in Ihrer Antwort Rückruf.

1) Sie verwenden JsonRequest von Volley, aber Sie möchten es tatsächlich in einem POJO mit Gson analysieren.

2) Sie Array von menurequest Objekte erhalten, aber es scheint, behandeln Sie dann nur als ein MenuRequest

so dass Sie nicht wirklich brauchen, um diese:

JSONObject jsonObject = new JSONObject(); 

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

Diese tatsächlich verschiedene Ausnahmen auslöst.

stattdessen können Sie tun:

MenuRequest[] items = gson.fromJson(response.toString(), MenuRequest[].class); 

so in Elemente, die Sie mit einem Array von MenuRequest wird Endup Objekte

dann können Sie tun, was Sie zum Beispiel benötigen:

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