简体   繁体   English

使用 Retrofit2 时获得 null 响应

[英]Getting null response when using Retrofit2

I am trying to login user and I am using Retrofit2 for networking but I am getting null response from the server though I have tested API in postman and it is showing response there but unable to fetch response in my app.我正在尝试登录用户并且我正在使用 Retrofit2 进行网络连接,但是我从服务器收到 null 响应,尽管我已经在 postman 中测试了 API 并且它在我的应用程序中显示响应但无法获取响应。 I am unable to discover the cause of this error.我无法发现此错误的原因。

Here is an API URL.这是 API URL。 You can check response where email=digi@gmail.com and password=digi1234:您可以查看 email=digi@gmail.com 和 password=digi1234 的回复:

http://www.gurgaonhomeo.in/api_server/login http://www.gurgaonhomeo.in/api_server/login

Below is my stack trace:下面是我的堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

This is my api response:这是我的 api 回复:

{
"data": [
    {
        "id": "7",
        "username": null,
        "mobile": "1254785698",
        "email": "digi@gmail.com",
        "image": "https://example.com",
        "created_date": "2020-11-06",
        "password": "dasdad324adad245435sffs34535",
        "name": "Digi",
        "address": "Hsbdbshbd",
        "homoeo_practioner": "Yes"
    }
],
"status": true,
"code": 200
}

These libraries I have used:我用过的这些库:

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0' 

LoginRespose.java登录Respose.java

public class LoginResponse {

@Expose
@SerializedName("code")
private String code;

@Expose
@SerializedName("data")
private List<LoginRes> data;

@Expose
@SerializedName("status")
private String status;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

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

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

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}
}

LoginRes.java登录Res.java

public class LoginRes {

@SerializedName("address")
@Expose
private String address;

@SerializedName("created_date")
@Expose
private String createdDate;

@SerializedName("email")
@Expose
private String email;

@SerializedName("homoeo_practioner")
@Expose
private String homoeoPractioner;

@SerializedName("id")
@Expose
private String id;

@SerializedName("image")
@Expose
private String image;

@SerializedName("mobile")
@Expose
private String mobile;

@SerializedName("name")
@Expose
private String name;

@SerializedName("password")
@Expose
private String password;

@SerializedName("username")
@Expose
private String username;

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(String createdDate) {
    this.createdDate = createdDate;
}

public String getEmail() {
    return email;
}

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

public String getHomoeoPractioner() {
    return homoeoPractioner;
}

public void setHomoeoPractioner(String homoeoPractioner) {
    this.homoeoPractioner = homoeoPractioner;
}

public String getId() {
    return id;
}

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

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getUsername() {
    return username;
}

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

ApiService.java ApiService.java

@POST("login")
@FormUrlEncoded
Call<LoginResponse> logUser(@Field("email") String email,
                            @Field("password") String password);

Login.java登录.java

 private void go(String  mail,String pwd){

    Retrofit retrofit = RetrofitClient.getInstance();
    ApiService apiService = retrofit.create(ApiService.class);

    apiService.logUser(mail,pwd).enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

            if(response.isSuccessful()){
                prg.dismiss();
               List<LoginRes> res = response.body().getData();
             Toast.makeText(getApplicationContext(),res.get(0).getEmail(),Toast.LENGTH_SHORT).show();
            }
        }

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

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

Why am I getting null response from the server?为什么我收到来自服务器的 null 响应?

First check if response.body() is null.首先检查response.body()是否为 null。 Then check if the data inside response is null or [].然后检查response里面的数据是null还是[]。 Then if those requirements are filled, then take the list and show the toast.然后,如果满足了这些要求,然后拿出清单并展示祝酒词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM