我正在尝试解析Android Studio中服务器的响应。 但是我没有在Arraylist中得到所有值。
我已经创建了ItemData.java和ItemResponse.java
我正在使用getItemData方法调用Api:
public void getItemData() {
Call<ItemDataResponse> call = service.getData(token, "200");
call.enqueue(new Callback<ItemDataResponse>() {
@Override
public void onResponse(@NonNull Call<ItemDataResponse> call, @NonNull Response<ItemDataResponse> response) {
Log.d("dataFromServer", response.body.toString());
}
@Override
public void onFailure(@NonNull Call<ItemDataResponse> call, @NonNull Throwable t) {
progressDoalog.dismiss();
Toast.makeText(LiveVehiclesActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
这是Json的回复:
{
"code":"1",
"items":[
{
"item_id":"200",
"speed":"2"
},
{
"item_id":"201",
"speed":"0"
}
]
}
ItemsResponse.java:
public class ItemDataResponse implements Serializable {
@SerializedName("code")
private String code;
@SerializedName("items")
@Expose
private List<ItemData> items;
public String getResponsecode() {
return responsecode;
}
public void setResponsecode(String responsecode) {
this.responsecode = responsecode;
}
public List<ItemData> getItems() {
return items;
}
public void setItems(List<ItemData> items) {
this.items = items;
}
}
ItemData.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ItemData {
@SerializedName("item_id")
@Expose
private String itemId;
@SerializedName("speed")
@Expose
private String speed;
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
}
在调试模式下运行后,我得到如下响应:
code:"1"
items=null
答案 0 :(得分:1)
您应该修改
class ItemData {
private String item_id;
private String speed;
public String getItem_id() {
return item_id;
}
public void setItem_id(String item_id) {
this.item_id = item_id;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
}
答案 1 :(得分:1)
ItemData模型似乎与JSON响应不一致。 即
{
"item_id":"200",
"speed":"2"
}
应映射到:
class ItemData implements Serializable {
@SerializedName("item_id")
private String itemId;
@SerializedName("speed")
private String speed;
// getters and setters
}
答案 2 :(得分:1)
使用SerializedName属性的字段包含“ _”。在这里,您的ItemData类中的item_id在映射时遇到问题。
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ItemData {
@SerializedName("item_id")
@Expose
private String itemId;
@SerializedName("speed")
@Expose
private String speed;
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
}
请也更改您的ItemDataResponse类
public class ItemDataResponse {
@SerializedName("code")
@Expose
private String code;
@SerializedName("items")
@Expose
private List<Item> items = null;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}