【问题标题】:Retrofit expected begin_array but was begin_object at line 1 column 2 path $ Android studio改造预期的 begin_array 但在第 1 行第 2 列路径 $ Android Studio 处是 begin_object
【发布时间】:2019-11-08 13:18:31
【问题描述】:

我知道这不是第一次有人问这个问题,但是使用 Retrofit2 我找不到解决问题的正确方法。我按照在线教程进行操作,效果很好。当我将相同的代码应用于我自己的端点时,我得到了这个异常:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $我不知道如何解决它

界面:

public interface LoginInterface {

    String BaseUrl = "https://411e5d9a-4b4f-4856-964a-f87759522aa7.mock.pstmn.io/";

    @GET("login")
    Call<List<UserInfos>> getUserInfos();


}

Java 类:

public class UserInfos {

private String firstName;
private String lastName;
private String email;
private String urlPhoto;
private String matricule;

public UserInfos(String firstName, String lastName, String email, String urlPhoto, String matricule) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.urlPhoto = urlPhoto;
    this.matricule = matricule;
}

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

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getUrlPhoto() {
    return urlPhoto;
}

public void setUrlPhoto(String urlPhoto) {
    this.urlPhoto = urlPhoto;
}

public String getMatricule() {
    return matricule;
}

public void setMatricule(String matricule) {
    this.matricule = matricule;
}
}

活动类:

public class AccueilActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accueil);

    fetchData();

}

public void fetchData(){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BaseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    LoginInterface loginInterface = retrofit.create(LoginInterface.class);

    Call<List<UserInfos>> call = loginInterface.getUserInfos();

    call.enqueue(new Callback<List<UserInfos>>() {
        @Override
        public void onResponse(Call<List<UserInfos>> call, Response<List<UserInfos>> response) {

            List<UserInfos> userInfos = response.body();

            for (UserInfos userInfos1: userInfos){

                Log.e("First name ---------",userInfos1.getFirstName());
                Log.e("Last name ---------",userInfos1.getLastName());
                Log.e("Email ---------",userInfos1.getEmail());
                Log.e("Matricule ---------",userInfos1.getMatricule());

            }
        }

        @Override
        public void onFailure(Call<List<UserInfos>> call, Throwable t) {

            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });

}

【问题讨论】:

    标签: android json rest retrofit


    【解决方案1】:
    LoginInterface loginInterface = retrofit.create(LoginInterface.class);
    
        loginInterface.login(firstname, lastname, email, matricule).enqueue(new Callback<UserInfos>() {
        @Override
        public void onResponse(Call<UserInfos> call, Response<UserInfos> response) {
            System.out.println("onResponse");
            System.out.println(response.body().toString());
        }
    
        @Override
        public void onFailure(Call<UserInfos> call, Throwable t) {
            System.out.println("onFailure");
            System.out.println(t.fillInStackTrace());
        }
     });
    

    【讨论】:

    • 我们没有登录方法,它给我一个错误 loginInterface.login(firstname, lastname, email, matricule)
    • 有解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多