我是新来的改造。我需要获取以下json数据。当我运行我的应用程序java.lang.ClassCastException: ad.app.retrofitexample.model.DS cannot be cast to retrofit2.Call
正在发生此类异常。请帮我解决这个问题。我在下面详细解释了我的代码。
{
"DS": {
"LST": [
{
"BID": 3,
"CNM": "kumar",
"BNO": "BLO41",
"VID": 1,
"ETI": 5,
"FRM": "/Date(1512930840000)/",
"TOD": "/Date(1513017240000)/",
"ENM": "Birthday Party",
"VNM": "murugan hall",
"CNO": "8888888888",
"AD1": "ram nagar",
"AD2": "ram colony",
"AD3": "polachi",
"CTN": "Coimbatore",
"CDT": "/Date(1512799571000)/",
"ICN": "0",
"SDT": "10/12/2017",
"EDT": "11/12/2017"
}
]
}
}
为此,我使用了pojo类
public class DS {
public DSObj getDsObj() {
return dsObj;
}
public void setDsObj(DSObj dsObj) {
this.dsObj = dsObj;
}
@SerializedName("DS")
private DSObj dsObj;
}
上面的模型类用于访问json对象" DS"
public class DSObj {
@SerializedName("LST")
private List<Booking> list;
public List<Booking> getList() {
return list;
}
public void setList(List<Booking> list) {
this.list = list;
}
}
上述类DSObj正在访问内部&#34; DS&#34; json对象。
public class Booking {
@SerializedName("CNM")
String customerName;
@SerializedName("BNO")
String bookingNumber;
@SerializedName("CNO")
String contactNumber;
@SerializedName("VNM")
String venueName;
@SerializedName("CTN")
String cityName;
@SerializedName("VID")
String vid;
public String getVenueName() {
return venueName;
}
public void setVenueName(String venueName) {
this.venueName = venueName;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getBookingNumber() {
return bookingNumber;
}
public void setBookingNumber(String bookingNumber) {
this.bookingNumber = bookingNumber;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getVid() {
return vid;
}
public void setVid(String vid) {
this.vid = vid;
}
}
上述模型类用于访问内部&#34; LST&#34; json数组。
The Api Interface class for retrofit is Kpublic interface ApiInterface {
String base_url = "http://195.100.1.103/mswed";
// listVenue
@GET("/?api=listBlocking&eid=5&vid=1&fdt=9/11/2017&tdt=9/01/2018&uid=1")
Call<DS> getVenueData();
}
以下代码正在初始化改造并获取数据。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bookingList = (ListView)findViewById(R.id.listData);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiInterface.base_url)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<DS> call = apiInterface.getVenueData();
call.enqueue(new Callback<DS>() {
@Override
public void onResponse(Call<DS> call, Response<DS> response) {
Call<DS> getList = (Call<DS>) response.body();
Log.e("Data",getList+"");
}
@Override
public void onFailure(Call<DS> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("Json Data Error",t.getMessage());
}
});
}
当我运行应用程序时抛出异常,我试过但我找不到解决方案。
答案 0 :(得分:3)
为了从给定的响应中获取Perticular字符串,你应该试试这个。
DS dsList= response.body().getDsObj();
List<Booking> mList= dsList.getList();
在mList
中你从Response获得了数组,现在你可以使用它来想要使用它。
答案 1 :(得分:1)
更改
Call<DS> getList = (Call<DS>) response.body();
到
DS getList = response.body();
答案 2 :(得分:0)
列出mList = new ArrayList(); mList.addAll(response.body()。getDsObj())
从列表中获取
mList.get(位置).getCustomerName.toString()