改造预期的 begin_array 但在第 1 行第 2 列路径 $ Android Studio 处是 begin_object

Posted

技术标签:

【中文标题】改造预期的 begin_array 但在第 1 行第 2 列路径 $ Android Studio 处是 begin_object【英文标题】:Retrofit expected begin_array but was begin_object at line 1 column 2 path $ Android studio 【发布时间】: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();

        
    );


【问题讨论】:

【参考方案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) 有解决办法吗?

以上是关于改造预期的 begin_array 但在第 1 行第 2 列路径 $ Android Studio 处是 begin_object的主要内容,如果未能解决你的问题,请参考以下文章

预期 BEGIN_ARRAY 但在第 1 行第 2 列是 BEGIN_OBJECT。如何解决?

快速 API 错误:预期 BEGIN_ARRAY 但 BEGIN_OBJECT 在第 1 行第 2 列 PATH $ 通过使用改造

使用改造调用服务时出错 Expected BEGIN_ARRAY 但在第 1 行第 1 列路径 $ 处为 STRING

预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT

获得错误预期BEGIN_ARRAY但在第1行第2列路径$是BEGIN_OBJECT

AsyncTask 预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY