当前位置: 首页 > 知识库问答 >
问题:

java.lang.IllegalStateException:应为BEGIN_ARRAY但为BEGIN_OBJECT-Android RETROFIT2

盖辉
2023-03-14

我已经在StackOverflow中多次看到这个问题。但这些答案没有解决我的问题,我对reverfit是新的。我将reverfit用于我的登录接口。我将发送用户名、密码,然后响应将是数组中的两个令牌。当我尝试登录时,日志猫显示java.lang.IllegalStateException:Expected BEGIN_ARRAY but was begin_object

请求

发帖:表单-已注册

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


  public void getUser(String username,String password){

        WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
        Call<UserResponse> call = apiService.getUsers("signin",username,password);
        call.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                UserResponse result = response.body();
                Log.d("res",""+result.getData());

            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Log.d("res",""+t.getMessage());

            }
        });
    }

模型类

public class User {

    String id;
    String username;
    String email;
    String access_token;
    String refresh_token;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

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

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getRefresh_token() {
        return refresh_token;
    }

    public void setRefresh_token(String refresh_token) {
        this.refresh_token = refresh_token;
    }

}

UserResponse.java

public class UserResponse {
    List<User> data;

    public List<User> getData() {
        return data;
    }

    public void setData(List<User> data) {
        this.data = data;
    }

}

接口

public interface WebserviceAPI {
    @FormUrlEncoded
    @POST("auth")
    Call<UserResponse> getUsers(@Field("module_action") String signin ,@Field("username") String username,@Field("password") String password);
}
$response = ['status' => true,
            'message' => "Successfully logged in",
            'data' => [
              'access_token' => $accessToken,
              'refresh_token' => $refreshToken
            ],
          ];
            $this->returnJson($response, 200);

{
    "status": false,
    "message": "Inactive User"
}

然后我想要获得message

我已按如下所示编辑userresponse并尝试获取消息。然后在com.android.app.myapp.login$3.onresponse(login.java:92)处显示java.lang.NullPointerException

 public class UserResponse {
        @SerializedName("status")
        @Expose
        private String status;
    
        @SerializedName("message")
        @Expose
        private String message;
    
        User data;
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public User getData() {
            return data;
        }
    
        public void setData(User data) {
            this.data = data;
        }
    
    }

public void getUser(String username,String password){

        WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
        Call<UserResponse> call = apiService.getUsers("signin",username,password);
        call.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                UserResponse result = response.body();
                //User data = result.getData();
                Log.d("userresponse",""+result.getMessage());
              
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Log.d("res",""+t.getMessage());

            }
        });
    }

共有1个答案

单于骁
2023-03-14

在邮递员响应中,数据字段是作为JSON对象出现的,而不是作为JSON数组出现的。如果是这种情况,并且您正在使用“数据”字段作为您的UserResponse模型中的列表,它将无法映射它。我认为这是问题所在。要解决这个问题,您可以使用以下UserResponse数据模型:

public class UserResponse {
    User data;

    public User getData() {
        return data;
    }

    public void setData(User data) {
        this.data = data;
    }

}
 类似资料: