我是 retrofit 新手。我需要获取下面的 json 数据。当我运行我的应用程序时java.lang.ClassCastException:ad.app.retrofitexample.model.DS无法转换为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 类正在访问内部“DS”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;
}
}
上面的模型类用于访问内部“LST”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());
}
});
}
当我运行应用程序时抛出异常,我尝试过但找不到解决方案。
最佳答案
为了从给定的响应中获取特定的字符串,你应该尝试这个。
DS dsList= response.body().getDsObj();
List<Booking> mList= dsList.getList();
在 mList
中,您从 Response 中获取数组,现在您可以随心所欲地使用它。
关于android - 如何使用retrofit获取Json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47766009/