Посмотрите мои данные API:
{
"items": [
{
"id": 2,
"sku": "Beige and blue jacquard and chiffon embroidered saree & Blouse",
"name": "Beige and blue jacquard and chiffon embroidered saree & Blouse",
"attribute_set_id": 4,
"price": 25.5,
"status": 1,
"visibility": 4,
"type_id": "simple",
"created_at": "2018-04-25 08:52:58",
"updated_at": "2018-05-23 19:00:20",
"product_links": [],
"tier_prices": [],
"custom_attributes": [
{
"attribute_code": "image",
"value": "/6/7/6759_original_1.jpg"
},
{
"attribute_code": "category_ids",
"value": [
"5",
"13"
]
}
]
}
],
"search_criteria": {
"filter_groups": []
},
"total_count": 574
}
И моя модель выглядит так:
-----------------------------------com.example
.apidata.models.CustomAttribute.java -----------------------------------
package com.example.apidata.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class CustomAttribute {
@SerializedName("attribute_code")
@Expose
private String attributeCode;
@SerializedName("value")
@Expose
private String value;
/**
* No args constructor for use in serialization
*
*/
public CustomAttribute() {
}
/**
*
* @param attributeCode
* @param value
*/
public CustomAttribute(String attributeCode, String value) {
super();
this.attributeCode = attributeCode;
this.value = value;
}
public String getAttributeCode() {
return attributeCode;
}
public void setAttributeCode(String attributeCode) {
this.attributeCode = attributeCode;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
-----------------------------------com.example.apidata.models.Item.java-----------------------------------
package com.example.apidata.models;
public class Item {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("name")
@Expose
private String name;
@SerializedName("attribute_set_id")
@Expose
private Integer attributeSetId;
@SerializedName("price")
@Expose
private float price;
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("visibility")
@Expose
private Integer visibility;
@SerializedName("type_id")
@Expose
private String typeId;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("product_links")
@Expose
private List<Object> productLinks = null;
@SerializedName("tier_prices")
@Expose
private List<Object> tierPrices = null;
@SerializedName("custom_attributes")
@Expose
private List<Object> customAttributes = null;
/**
* No args constructor for use in serialization
*
*/
public Item() {
}
/**
*
* @param productLinks
* @param updatedAt
* @param id
* @param price
* @param attributeSetId
* @param visibility
* @param status
* @param createdAt
* @param name
* @param tierPrices
* @param sku
* @param typeId
* @param customAttributes
*/
public Item(Integer id, String sku, String name, Integer attributeSetId, Integer price, Integer status, Integer visibility, String typeId, String createdAt, String updatedAt, List<Object> productLinks, List<Object> tierPrices, List<Object> customAttributes) {
super();
this.id = id;
this.sku = sku;
this.name = name;
this.attributeSetId = attributeSetId;
this.price = price;
this.status = status;
this.visibility = visibility;
this.typeId = typeId;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.productLinks = productLinks;
this.tierPrices = tierPrices;
this.customAttributes = customAttributes;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAttributeSetId() {
return attributeSetId;
}
public void setAttributeSetId(Integer attributeSetId) {
this.attributeSetId = attributeSetId;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getVisibility() {
return visibility;
}
public void setVisibility(Integer visibility) {
this.visibility = visibility;
}
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public List<Object> getProductLinks() {
return productLinks;
}
public void setProductLinks(List<Object> productLinks) {
this.productLinks = productLinks;
}
public List<Object> getTierPrices() {
return tierPrices;
}
public void setTierPrices(List<Object> tierPrices) {
this.tierPrices = tierPrices;
}
public List<Object> getCustomAttributes() {
return customAttributes;
}
public void setCustomAttributes(List<Object> customAttributes) {
this.customAttributes = customAttributes;
}
}
----------------------------------- com.example.apidata.models.Result.java ---- -------------------------------
package com.example.apidata.models;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("items")
@Expose
private List<Item> items = null;
@SerializedName("search_criteria")
@Expose
private SearchCriteria searchCriteria;
@SerializedName("total_count")
@Expose
private Integer totalCount;
/**
* No args constructor for use in serialization
*
*/
public Result() {
}
/**
*
* @param totalCount
* @param searchCriteria
* @param items
*/
public Result(List<Item> items, SearchCriteria searchCriteria, Integer totalCount) {
super();
this.items = items;
this.searchCriteria = searchCriteria;
this.totalCount = totalCount;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public SearchCriteria getSearchCriteria() {
return searchCriteria;
}
public void setSearchCriteria(SearchCriteria searchCriteria) {
this.searchCriteria = searchCriteria;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
}
-----------------------------------com.example.apidata.models.SearchCriteria.java-----------------------------------
package com.example.apidata.models;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class SearchCriteria {
@SerializedName("filter_groups")
@Expose
private List<Object> filterGroups = null;
/**
* No args constructor for use in serialization
*
*/
public SearchCriteria() {
}
/**
*
* @param filterGroups
*/
public SearchCriteria(List<Object> filterGroups) {
super();
this.filterGroups = filterGroups;
}
public List<Object> getFilterGroups() {
return filterGroups;
}
public void setFilterGroups(List<Object> filterGroups) {
this.filterGroups = filterGroups;
}
}
Я получаю данные в представлении рециркулятора, используя адаптер, но выдает ошибку как:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Ожидаемый BEGIN_ARRAY, но был STRING в строке 1 пути 408
$ .items [0] .custom_attributes [0] .value
Это дает мне значение массива " items ", но не дает значения массива " custom_attributes ", который фактически находится внутри массива " items"
Вот мой класс Адаптера:
public class Adapter extends RecyclerView.Adapter{
private Context context;
private List<Item> data = Collections.emptyList();
public Adapter(List<Item> data)
{
this.data = data;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.grid,parent,false);
return new ProgrammingViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final Item result = data.get(position);
((ProgrammingViewHolder)holder).name.setText(result.getName());
((ProgrammingViewHolder)holder).price.setText((int) result.getPrice());
((ProgrammingViewHolder)holder).image.setText(result.getCustomAttributes().toArray(););
}
@Override
public int getItemCount() {
return data.size();
}
public class ProgrammingViewHolder extends RecyclerView.ViewHolder{
TextView name, price;
ImageView image;
public ProgrammingViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.textView1);
price = itemView.findViewById(R.id.textView2);
image = itemView.findViewById(R.id.imageView);
}
}
}
MainActivity.java:
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response", response);
results = gson.fromJson(response, Result.class);
data = results.getItems();
Log.d("data", String.valueOf(data));
adapter = new Adapter(data);
programming_list.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("Authorization", "API KEY");
return headers;
}
};
requestQueue.add(stringRequest);
Итак, пожалуйста, помогите мне получить значение массива " custom_attributes "