Udhay Walker Udhay Walker - 26 days ago 7
Java Question

How to get the json data from server in recycler view in Android using Retrofit?

I am using Retrofit for parsing data in recycler view. I am not knowledgable about Retrofit.

My Json Fromat:

[{"id":3106,"sku":"62974","name":"NESTLE CERELAC STG 1 WHEAT 300G","attribute_set_id":4,"price":164,"status":1,"visibility":4,"type_id":"simple","created_at":"2017-08-16 16:15:30","updated_at":"2017-09-14 06:54:36","extension_attributes":{"stock_item":{"item_id":5627,"product_id":3106,"stock_id":1,"qty":3,"is_in_stock":true,"is_qty_decimal":false,"show_default_notification_message":false,"use_config_min_qty":true,"min_qty":0,"use_config_min_sale_qty":1,"min_sale_qty":1,"use_config_max_sale_qty":true,"max_sale_qty":10000,"use_config_backorders":true,"backorders":0,"use_config_notify_stock_qty":true,"notify_stock_qty":1,"use_config_qty_increments":true,"qty_increments":0,"use_config_enable_qty_inc":true,"enable_qty_increments":false,"use_config_manage_stock":true,"manage_stock":true,"low_stock_date":null,"is_decimal_divided":false,"stock_status_changed_auto":0}},"product_links":[],"options":[],"media_gallery_entries":[{"id":1127,"media_type":"image","label":"","position":1,"disabled":false,"types":["image","small_image","thumbnail","swatch_image"],"file":"\/6\/2\/62974.png"}],"tier_prices":[],"custom_attributes":[{"attribute_code":"description","value":"
NESTLE CERELAC STG 1 WHEAT 300G<\/p>"},{"attribute_code":"short_description","value":"

NESTLE CERELAC STG 1 WHEAT 300G<\/p>"},{"attribute_code":"special_price","value":"160.7200"},{"attribute_code":"special_from_date","value":"2017-08-17 20:17:57"},{"attribute_code":"meta_title","value":"NESTLE CERELAC STG 1 WHEAT 300G"},{"attribute_code":"meta_description","value":"NESTLE CERELAC STG 1 WHEAT 300G"},{"attribute_code":"image","value":"\/6\/2\/62974.png"},{"attribute_code":"small_image","value":"\/6\/2\/62974.png"},{"attribute_code":"thumbnail","value":"\/6\/2\/62974.png"},{"attribute_code":"news_from_date","value":"2017-08-17 20:17:57"},{"attribute_code":"custom_design_from","value":"2017-08-17 20:17:57"},{"attribute_code":"category_ids","value":["56","631"]},{"attribute_code":"options_container","value":"container2"},{"attribute_code":"required_options","value":"0"},{"attribute_code":"has_options","value":"0"},{"attribute_code":"msrp_display_actual_price_type","value":"0"},{"attribute_code":"url_key","value":"nestle-cerelac-stg-1-wheat-300g"},{"attribute_code":"gift_message_available","value":"2"},{"attribute_code":"tax_class_id","value":"2"},{"attribute_code":"swatch_image","value":"\/6\/2\/62974.png"}]}


i have get the name, sku, id successfully using below code:

MainActivity:

public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private RecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle_retrofit);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
// recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
requestJsonObject();
}
private void requestJsonObject(){
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://alagendransupermart.com/mageapi/cat_product.php?cid=83";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response " + response);
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<ItemObject> posts = new ArrayList<ItemObject>();
posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));
adapter = new RecyclerViewAdapter(MainActivity.this, posts);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error " + error.getMessage());
}
});
queue.add(stringRequest);
}
}


Adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_retrofit, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.songTitle.setText("Product Name: " + itemList.get(position).getSongTitle());
holder.songYear.setText("ID: " + itemList.get(position).getSongYear());
holder.songAuthor.setText("SKU: " + itemList.get(position).getSongAuthor());
}
@Override
public int getItemCount() {
return this.itemList.size();
}
}


Getter and setter:

public class ItemObject {
@SerializedName("name")
private String songTitle;
@SerializedName("id")
private String songYear;
@SerializedName("sku")
private String songAuthor;



public ItemObject(String songTitle, String songYear, String songAuthor) {
this.songTitle = songTitle;
this.songYear = songYear;
this.songAuthor = songAuthor;
}
public String getSongTitle() {
return songTitle;
}
public String getSongYear() {
return songYear;
}
public String getSongAuthor() {
return songAuthor;
}
}


But how can I get the values inside the next array name like parameters and there are more than 5 attributes with same name but with different values.

Answer Source

You should add to ItemObject :

@SerializedName("extension_attributes")
private ExtensionAttributes extensionAttributes;

ExtensionAttributes.java:

public class ExtensionAttributes {

    @SerializedName("stock_item")
    @Expose
    private StockItem stockItem;

    public StockItem getStockItem() {
        return stockItem;
    }

    public void setStockItem(StockItem stockItem) {
        this.stockItem = stockItem;
    }

}

StockItem.java:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class StockItem {

    @SerializedName("item_id")
    @Expose
    private Integer itemId;
    @SerializedName("product_id")
    @Expose
    private Integer productId;
    @SerializedName("stock_id")
    @Expose
    private Integer stockId;
    @SerializedName("qty")
    @Expose
    private Integer qty;
    @SerializedName("is_in_stock")
    @Expose
    private Boolean isInStock;
    @SerializedName("is_qty_decimal")
    @Expose
    private Boolean isQtyDecimal;
    @SerializedName("show_default_notification_message")
    @Expose
    private Boolean showDefaultNotificationMessage;
    @SerializedName("use_config_min_qty")
    @Expose
    private Boolean useConfigMinQty;
    @SerializedName("min_qty")
    @Expose
    private Integer minQty;
    @SerializedName("use_config_min_sale_qty")
    @Expose
    private Integer useConfigMinSaleQty;
    @SerializedName("min_sale_qty")
    @Expose
    private Integer minSaleQty;
    @SerializedName("use_config_max_sale_qty")
    @Expose
    private Boolean useConfigMaxSaleQty;
    @SerializedName("max_sale_qty")
    @Expose
    private Integer maxSaleQty;
    @SerializedName("use_config_backorders")
    @Expose
    private Boolean useConfigBackorders;
    @SerializedName("backorders")
    @Expose
    private Integer backorders;
    @SerializedName("use_config_notify_stock_qty")
    @Expose
    private Boolean useConfigNotifyStockQty;
    @SerializedName("notify_stock_qty")
    @Expose
    private Integer notifyStockQty;
    @SerializedName("use_config_qty_increments")
    @Expose
    private Boolean useConfigQtyIncrements;
    @SerializedName("qty_increments")
    @Expose
    private Integer qtyIncrements;
    @SerializedName("use_config_enable_qty_inc")
    @Expose
    private Boolean useConfigEnableQtyInc;
    @SerializedName("enable_qty_increments")
    @Expose
    private Boolean enableQtyIncrements;
    @SerializedName("use_config_manage_stock")
    @Expose
    private Boolean useConfigManageStock;
    @SerializedName("manage_stock")
    @Expose
    private Boolean manageStock;
    @SerializedName("low_stock_date")
    @Expose
    private Object lowStockDate;
    @SerializedName("is_decimal_divided")
    @Expose
    private Boolean isDecimalDivided;
    @SerializedName("stock_status_changed_auto")
    @Expose
    private Integer stockStatusChangedAuto;

    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public Integer getStockId() {
        return stockId;
    }

    public void setStockId(Integer stockId) {
        this.stockId = stockId;
    }

    public Integer getQty() {
        return qty;
    }

    public void setQty(Integer qty) {
        this.qty = qty;
    }

    public Boolean getIsInStock() {
        return isInStock;
    }

    public void setIsInStock(Boolean isInStock) {
        this.isInStock = isInStock;
    }

    public Boolean getIsQtyDecimal() {
        return isQtyDecimal;
    }

    public void setIsQtyDecimal(Boolean isQtyDecimal) {
        this.isQtyDecimal = isQtyDecimal;
    }

    public Boolean getShowDefaultNotificationMessage() {
        return showDefaultNotificationMessage;
    }

    public void setShowDefaultNotificationMessage(Boolean showDefaultNotificationMessage) {
        this.showDefaultNotificationMessage = showDefaultNotificationMessage;
    }

    public Boolean getUseConfigMinQty() {
        return useConfigMinQty;
    }

    public void setUseConfigMinQty(Boolean useConfigMinQty) {
        this.useConfigMinQty = useConfigMinQty;
    }

    public Integer getMinQty() {
        return minQty;
    }

    public void setMinQty(Integer minQty) {
        this.minQty = minQty;
    }

    public Integer getUseConfigMinSaleQty() {
        return useConfigMinSaleQty;
    }

    public void setUseConfigMinSaleQty(Integer useConfigMinSaleQty) {
        this.useConfigMinSaleQty = useConfigMinSaleQty;
    }

    public Integer getMinSaleQty() {
        return minSaleQty;
    }

    public void setMinSaleQty(Integer minSaleQty) {
        this.minSaleQty = minSaleQty;
    }

    public Boolean getUseConfigMaxSaleQty() {
        return useConfigMaxSaleQty;
    }

    public void setUseConfigMaxSaleQty(Boolean useConfigMaxSaleQty) {
        this.useConfigMaxSaleQty = useConfigMaxSaleQty;
    }

    public Integer getMaxSaleQty() {
        return maxSaleQty;
    }

    public void setMaxSaleQty(Integer maxSaleQty) {
        this.maxSaleQty = maxSaleQty;
    }

    public Boolean getUseConfigBackorders() {
        return useConfigBackorders;
    }

    public void setUseConfigBackorders(Boolean useConfigBackorders) {
        this.useConfigBackorders = useConfigBackorders;
    }

    public Integer getBackorders() {
        return backorders;
    }

    public void setBackorders(Integer backorders) {
        this.backorders = backorders;
    }

    public Boolean getUseConfigNotifyStockQty() {
        return useConfigNotifyStockQty;
    }

    public void setUseConfigNotifyStockQty(Boolean useConfigNotifyStockQty) {
        this.useConfigNotifyStockQty = useConfigNotifyStockQty;
    }

    public Integer getNotifyStockQty() {
        return notifyStockQty;
    }

    public void setNotifyStockQty(Integer notifyStockQty) {
        this.notifyStockQty = notifyStockQty;
    }

    public Boolean getUseConfigQtyIncrements() {
        return useConfigQtyIncrements;
    }

    public void setUseConfigQtyIncrements(Boolean useConfigQtyIncrements) {
        this.useConfigQtyIncrements = useConfigQtyIncrements;
    }

    public Integer getQtyIncrements() {
        return qtyIncrements;
    }

    public void setQtyIncrements(Integer qtyIncrements) {
        this.qtyIncrements = qtyIncrements;
    }

    public Boolean getUseConfigEnableQtyInc() {
        return useConfigEnableQtyInc;
    }

    public void setUseConfigEnableQtyInc(Boolean useConfigEnableQtyInc) {
        this.useConfigEnableQtyInc = useConfigEnableQtyInc;
    }

    public Boolean getEnableQtyIncrements() {
        return enableQtyIncrements;
    }

    public void setEnableQtyIncrements(Boolean enableQtyIncrements) {
        this.enableQtyIncrements = enableQtyIncrements;
    }

    public Boolean getUseConfigManageStock() {
        return useConfigManageStock;
    }

    public void setUseConfigManageStock(Boolean useConfigManageStock) {
        this.useConfigManageStock = useConfigManageStock;
    }

    public Boolean getManageStock() {
        return manageStock;
    }

    public void setManageStock(Boolean manageStock) {
        this.manageStock = manageStock;
    }

    public Object getLowStockDate() {
        return lowStockDate;
    }

    public void setLowStockDate(Object lowStockDate) {
        this.lowStockDate = lowStockDate;
    }

    public Boolean getIsDecimalDivided() {
        return isDecimalDivided;
    }

    public void setIsDecimalDivided(Boolean isDecimalDivided) {
        this.isDecimalDivided = isDecimalDivided;
    }

    public Integer getStockStatusChangedAuto() {
        return stockStatusChangedAuto;
    }

    public void setStockStatusChangedAuto(Integer stockStatusChangedAuto) {
        this.stockStatusChangedAuto = stockStatusChangedAuto;
    }

}

I hope you can use this instrument next time.