改造中出现错误call.enqueue

时间:2018-06-19 17:27:53

标签: android retrofit

我使用Retrofit简单
我在通话中出现错误“ <” EmployeesModel“>”

并且MyApplictaion与Run一起显示停止执行消息 MySource:

 Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://www.mocky.io/v2/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    APIService service = retrofit.create(APIService.class);

和服务器源:

 //Server
    Call<EmployeesModel> call = service.getEmployees();
    call.enqueue(new Callback<EmployeesModel>() {

        @Override
        public void onResponse(Call<EmployeesModel> call, Response<EmployeesModel> response) {
            Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<EmployeesModel> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
        }
    });

我的界面来源

 public interface APIService {

    @GET("569ce520110000fb2dce7655")
    Call<EmployeesModel> getEmployees();
}

1 个答案:

答案 0 :(得分:1)

将您的EmployeesModel模型替换为下面的模型,并添加另一个名为Employee的模型类

EmployeesModel.java

    public class EmployeesModel {

@SerializedName("employees")
@Expose
private List<Employee> employees = null;

public List<Employee> getEmployees() {
return employees;
}

public void setEmployees(List<Employee> employees) {
this.employees = employees;
}

}

Employee.java

    public class Employee {

@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;

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;
}

}

然后尝试像这样访问数据(示例显示访问密钥-firstName

@Override
    public void onResponse(Call<EmployeesModel> call, Response<EmployeesModel> response) {
        Toast.makeText(MainActivity.this, "OK, firstName :"+response.body().getFirstName(), Toast.LENGTH_SHORT).show();
    }