我正在使用改造来从我的API接收JSON数据。当数据包含单个对象时,我能够接收JSON数据。但当我尝试获取所有员工的清单时,我收到了错误。
/ api / Employee / 1的JSON数据是(正常工作):
{
"id": 10,
"name": "Arpit",
"number": "7777777777",
"city": "noida",
"depid": 4,
}
获取此JSON对象的代码正在运行。 PFB代码。
改造代码:
public void onResponse(Response<EmployeeResponse> response, Retrofit retrofit) {
if (response.raw().toString()!=null) {
Employee e = new Employee();
e.setId(response.body().id);
e.setName(response.body().name);
e.setCity(response.body().city);
e.setNumber(response.body().number);
Log.d("employee details", e.toString());
list.add(e);
rv.setAdapter(new SimpleItemRecyclerViewAdapter(list));
}
else
Log.d("No data recieved",response.raw().toString());
}
@Override
public void onFailure(Throwable t) {
Log.d("Fail",t.getMessage());}
EmployeeAPI.java
public interface EmployeeAPI {
@GET("/assignment1/api/Employee/1")
Call<EmployeeResponse> load_employee();}
EmployeeResponse.java
public class EmployeeResponse {
@SerializedName("id")
public String id;
@SerializedName("name")
public String name;
@SerializedName("city")
public String city;
@SerializedName("number")
public String number;}
但是当我试图从/ api / Employee获取所有员工的列表时,我收到以下错误:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
api / Employee的JSON数据是():
[
{
"id":10,
"name":"Arpit Jain",
"number":"7777777777",
"city":"noida",
"depid":4,
},
{
"id":32,
"name":"Ish Chawla",
"number":"9999999999",
"city":"Noida",
"depid":1,
},
{
"id":39,
"name":"Laxman",
"number":"6666666666",
"city":"GZB",
"depid":1,
},
{
"id":44,
"name":"Ishan",
"number":"9839060055",
"city":"Kanpur",
"depid":3,
}
]
获取员工名单的代码是:
改造代码
public void onResponse(Response<EmployeeResponse> response, Retrofit retrofit) {
if (response.raw().toString()!=null) {
Log.d("employee details", response.body().toString());
list = response.body().list;
rv.setAdapter(new SimpleItemRecyclerViewAdapter(list));
}
else
Log.d("No data recieved",response.raw().toString());
}
@Override
public void onFailure(Throwable t) {
Log.d("Fail",t.getMessage());
}
EmployeeAPI.java
public interface EmployeeAPI {
@GET("/assignment1/api/Employee")
Call<EmployeeResponse> load_employee();
}
EmployeeResponse.java
public class EmployeeResponse {
public ArrayList<Employee> list;
}
请告诉我正确获取数据所需的更改。
答案 0 :(得分:1)
因为您正在获取整个员工列表及其数组。
public interface EmployeeAPI {
@GET("/assignment1/api/Employee")
Call<Employee[]> load_employee();
@GET("/assignment1/api/Employee/1")
Call<Employee> load_employee();
}
并且不要创建EmployeeResponse类。在Employee pojo类中使用@SerializedName。
收到回复后
if (response.raw().toString()!=null) {
Employee e = new Employee();
e.setId(response.body().id);
e.setName(response.body().name);
e.setCity(response.body().city);
e.setNumber(response.body().number);
Log.d("employee details", e.toString());
list.add(e);
rv.setAdapter(new SimpleItemRecyclerViewAdapter(list));
}
不需要设置它。
答案 1 :(得分:0)
public class EmployeeResponse {
@SerializedName("id")
public String id;
@SerializedName("name")
public String name;
@SerializedName("city")
public String city;
@SerializedName("number")
public String number;
}
使用上面的类作为模型
public interface EmployeeAPI {
@GET("/assignment1/api/Employee")
Call<ArrayList<EmployeeResponse>> load_employee();
}
并且由于您返回列表,因此调用对象接受列表以接受员工列表
答案 2 :(得分:0)
您需要在代码中进行少量更改:
1)的 EmployeeResponse.java 强>
public class EmployeeResponse {
public List<Employee> list;
public List<Employee> getDatas() {
return datas;
}
}
2) Employee.java
public class Employee{
@SerializedName("id")
public String id;
@SerializedName("name")
public String name;
@SerializedName("city")
public String city;
@SerializedName("number")
public String number;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
3)在界面中:
public interface EmployeeAPI {
@GET("/assignment1/api/Employee")
Call<EmployeeResponse> load_employee();
}
4)改造代码
private void getEmployee() {
List<Employee> employee;
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
EmployeeAPI apiInterface = retrofit.create(EmployeeAPI.class);
Call<EmployeeResponse> call = apiInterface.load_employee();
call.enqueue(new Callback<EmployeeResponse>() {
@Override
public void onResponse(Call<EmployeeResponse> call, Response<EmployeeResponse> response) {
employee = response.body().getDatas();
}
@Override
public void onFailure(Call<EmployeeResponse> call, Throwable t) {
Log.e(TAG, "onFailure:" + t.getMessage());
}
});
}