我正在使用 Retrofit 与 Android 上的 REST API 进行通信,但收到如下错误 NullPointerException。我尝试使用 postman ,API 工作正常,我得到了响应。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List ukmutilizer.project.com.ukm_utilizer.model.CheckEmail.getData()' on a null object reference
这是我的 Activity 类(class)
private void sendRequest(String checkEmail){
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<CheckEmail> call = apiService.getEmailStatus(checkEmail);
call.enqueue(new Callback<CheckEmail>() {
@Override
public void onResponse(Call<CheckEmail> call, Response<CheckEmail> response) {
CheckEmailData emailDataList = response.body().getData();
Log.d("Numer of Data : ", String.valueOf(response.body().getData()));
}
@Override
public void onFailure(Call<CheckEmail> call, Throwable t) {
Toast.makeText(CheckEmailPage.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
Log.e("Error Retrofit : ", String.valueOf(t));
}
});
这是Api接口(interface)
public interface ApiInterface {
@POST("users/check_status")
Call<CheckEmail> getEmailStatus(@Body String email);
}
这是改造实例
`public class ApiClient {
public static final String BASE_URL = "https://f49d9d29-8471-4126-95b0-1ec3d18eda94.mock.pstmn.io/v1/";
private static Retrofit retrofit = null;
public static Retrofit getClient(){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}`
这是 json 响应
{
"code": 1000,
"message": "OK",
"data": {
"id": "1",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f286978186b2959f939b9edc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>",
"status": "1",
"name": "test",
"category": "2"
}
}
这是 POJO
`public class CheckEmail {
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private CheckEmailData data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public CheckEmailData getData() {
return data;
}
public void setData(CheckEmailData data) {
this.data = data;
}
}`
检查电子邮件数据 POJO
`public class CheckEmailData {
@SerializedName("id")
@Expose
private String id;
@SerializedName("email")
@Expose
private String email;
@SerializedName("status")
@Expose
private String status;
@SerializedName("name")
@Expose
private String name;
@SerializedName("category")
@Expose
private String category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}`
最佳答案
在 json 响应中,您仅从 api 获得了一个数据,即 json 对象
{
"code": 1000,
"message": "OK",
"data": {
"id": "1",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c68796f685c7b717d7570327f7371" rel="noreferrer noopener nofollow">[email protected]</a>",
"status": "1",
"name": "test",
"category": "2"
}
}
但是您将数据声明为 List
对象,它期望上面的 data
采用 json 数组格式。
您应该将列表更改为
@SerializedName("data")
@Expose
private CheckEmailData data;
我相信这会没问题。
关于java - 使用 Retrofit 从 REST 获取响应时出现 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49204531/