我在Android上使用Retrofit与REST API进行通信,但是我收到错误NullPointerException,如下所示。我尝试使用postman,API工作正常,我得到了响应。
java.lang.NullPointerException:尝试调用虚方法' java.util.List ukmutilizer.project.com.ukm_utilizer.model.CheckEmail.getData()'在空对象引用上
这是我的Activity类
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));
}
});
这是ApiInterface
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": "test@gmail.com",
"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;
}
}`
CheckEmailData 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;
}
}`
答案 0 :(得分:1)
您从json响应中的api只获得了一个数据json对象
{
"code": 1000,
"message": "OK",
"data": {
"id": "1",
"email": "test@gmail.com",
"status": "1",
"name": "test",
"category": "2"
}
}
然而你将数据声明为List
对象,它在json数组格式中期望上面的data
。
您应该将列表更改为
@SerializedName("data")
@Expose
private CheckEmailData data;
我相信会没事的。
答案 1 :(得分:1)
在那个JSON中,“data”是一个对象,而不是一个数组。在CheckEmail
课程中,将private List<CheckEmailData> data;
更改为private CheckEmailData data;
答案 2 :(得分:0)
在访问响应中的数据之前,您应该检查响应本身是否正常。
getSupportActionBar().setTitle("ACTIVITY NAME");
答案 3 :(得分:0)
我已经解决了这个问题
我在界面上添加了标题,如下所示
public interface ApiInterface {
@Headers({
"Content-Type:application/x-www-form-urlencoded"
})
@POST("v1/users/check_status")
Call<CheckEmail> getEmailStatus(@Body String email);
}