这个错误使我发疯,我尝试使用诸如JsonArrayRequest
和JsonObjectRequest
之类的不同Volley请求,但是没有运气!
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但位于第1行第2列路径的BEGIN_OBJECT $
这是JSON响应链接=> http://www.mocky.io/v2/58df83a80f00005922eaf4e1
这是我的模特班
public class Order {
@SerializedName("status")
@Expose
private String status; //status
@SerializedName("total")
@Expose
private float price; //total
@SerializedName("created_at")
@Expose
private Date time;
@SerializedName("line_items")
@Expose
public List<ItemsNum> lineItems = new ArrayList<ItemsNum>();
@SerializedName("sub_orders")
@Expose
public List<OrdersNum> subOrders = new ArrayList<OrdersNum>();
@SerializedName("shipping_address")
@Expose
public CusAddress cusAdress;
@SerializedName("customer")
@Expose
public Customer customer;
public Order(Customer customer, String status, float price, String cusPicURL, String firstName, String lastName, Date time, List<ItemsNum> lineItems, List<OrdersNum> subOrders, CusAddress cusAdress) {
this.status = status;
this.price = price;
this.customer = customer;
this.time = time;
this.lineItems = lineItems;
this.subOrders = subOrders;
this.cusAdress = cusAdress;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public List<ItemsNum> getLineItems() {
return lineItems;
}
public void setLineItems(List<ItemsNum> lineItems) {
this.lineItems = lineItems;
}
public List<OrdersNum> getSubOrders() {
return subOrders;
}
public void setSubOrders(List<OrdersNum> subOrders) {
this.subOrders = subOrders;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public CusAddress getCusAdress() {
return cusAdress;
}
public void setCusAdress(CusAddress cusAdress) {
this.cusAdress = cusAdress;
}
// //////////// Inner-Classes
public class Customer {
@SerializedName("avatar_url")
@Expose
private String cusPicURL; //avatar_url
@SerializedName("first_name")
@Expose
private String firstName; //first_name
@SerializedName("last_name")
@Expose
private String lastName; //last_name
public String getCusPicURL() {
return cusPicURL;
}
public void setCusPicURL(String cusPicURL) {
this.cusPicURL = cusPicURL;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class CusAddress {
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@SerializedName("address_1")
@Expose
public String address;
@SerializedName("city")
@Expose
public String city;
@SerializedName("state")
@Expose
public String state;
@SerializedName("country")
@Expose
public String country;
}
public class ItemsNum {
@SerializedName("id")
@Expose
public Integer id;
}
public class OrdersNum {
@SerializedName("id")
@Expose
public Integer id;
}
}
我的适配器
public class OrdersDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public Context context;
private int num;
ArrayList<Order> orders;
public OrdersDataAdapter(ArrayList<Order> orders, Context context) {
this.orders = orders;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_card, parent, false);
return new NewOrderVH(v);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final Order order = this.orders.get(position);
final NewOrderVH vh1 = (NewOrderVH) holder;
vh1.setData(orders.get(position));
}
@Override
public int getItemCount() {
return orders.size();
}
public class NewOrderVH extends RecyclerView.ViewHolder {
private ImageView cusPic;
private TextView cusName;
private TextView CusAdress;
private TextView vendorsNum;
private TextView itemsNum;
private TextView time;
public NewOrderVH(View itemView) {
super(itemView);
orderCard = (CardView) itemView.findViewById(R.id.OrderCard);
cusPic = (ImageView) itemView.findViewById(R.id.cusPic);
cusName = (TextView) itemView.findViewById(R.id.cusName);
CusAdress = (TextView) itemView.findViewById(R.id.CusAdress);
vendorsNum = (TextView) itemView.findViewById(R.id.vendorsNum);
itemsNum = (TextView) itemView.findViewById(R.id.itemsNum);
time = (TextView) itemView.findViewById(R.id.time);
}
public void setData(final Order data) {
cusName.setText(data.customer.getFirstName() + "" + data.customer.getLastName());
Picasso.with(context).load(data.customer.getCusPicURL()).into(cusPic);
time.setText(data.getTime().getMinutes());
vendorsNum.setText(data.getSubOrders().size());
itemsNum.setText(data.getLineItems().size());
CusAdress.setText(data.cusAdress.getAddress() + "" + data.cusAdress.getCity() + "" + data.cusAdress.getCountry() + "" + data.cusAdress.getState());
}
}
}
片段内的请求方法
private void fetchPosts() {
requestQueue = Volley.newRequestQueue(getActivity());
StringRequest jRequest = new StringRequest(Request.Method.GET, ENDPOINT, onPostsLoaded, onPostsError);
requestQueue.add(jRequest);
}
private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
Type listType = new TypeToken<List<Order>>() {}.getType();
modelData = gson.fromJson(response, listType);
adapter = new OrdersDataAdapter((ArrayList<Order>) modelData, getActivity());
recyclerView.setAdapter(adapter);
adapter.setMode(Attributes.Mode.Single);
}
};
private final Response.ErrorListener onPostsError = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("PostActivity", error.toString());
}
};
最佳答案
在这里,您的问题是orders
对象。您不反序列化orders
。你可以解决这个问题
public class OrderHolder{
private List<Order> orders;
public List<Order> getOrders(){ return orders; }
}
OrderHolder holder = new Gson().fromJson(response, OrderHolder.class);
modelData = holder.getOrders();
要么
JsonElement jelement = new JsonParser().parse(response);
JsonObject jobject = jelement.getAsJsonObject();
String ordersString = jobject.get("orders").getAsString();
Gson gson = new Gson();
Type listType = new TypeToken<List<Order>>() {}.getType();
modelData = gson.fromJson(ordersString, listType);