【问题标题】:How to get the json data from server in recycler view in Android using Retrofit?如何使用 Retrofit 在 Android 的回收站视图中从服务器获取 json 数据?
【发布时间】:2017-09-23 10:49:56
【问题描述】:

我正在使用 Retrofit 在回收站视图中解析数据。我对改造一窍不通。

我的 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"}]}

我已使用以下代码成功获取名称、sku、id:

主活动:

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

适配器:

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

但是如何获取下一个数组名称中的值,如参数,并且有超过 5 个名称相同但值不同的属性。

【问题讨论】:

  • 您是在问如何在“extension_attributes”中获取“stock_item”?
  • 没错……

标签: java android json retrofit


【解决方案1】:

你应该添加到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;
    }

}

希望你下次可以使用this instrument

【讨论】:

  • Thq 它很有用,但请将我的 mainactivity 的编码发送给我,以便在我的 Recycler 视图中设置此值,请帮助我...
  • @UdhayWalker 请在另一个单独的问题中询问另一个问题
  • 请它不是下一个问题,我要求为同一个问题进行 mainactivity 编码
猜你喜欢
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2017-06-12
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 2019-12-31
相关资源
最近更新 更多