Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用2时获取空响应_Java_Android_Retrofit2 - Fatal编程技术网

Java 使用2时获取空响应

Java 使用2时获取空响应,java,android,retrofit2,Java,Android,Retrofit2,我正在尝试登录用户,我正在使用Reformation2进行网络连接,但我从服务器收到空响应,尽管我在postman中测试了api,它在那里显示响应,但无法在我的应用程序中获取响应。我无法获取此错误的原因,我一直在努力解决此问题 这是一个Api URL 这是一个api,您可以在电子邮件中查看响应=digi@gmail.com密码=digi1234 下面是我的堆栈跟踪: java.lang.NullPointerException: Attempt to invoke interface metho

我正在尝试登录用户,我正在使用Reformation2进行网络连接,但我从服务器收到空响应,尽管我在postman中测试了api,它在那里显示响应,但无法在我的应用程序中获取响应。我无法获取此错误的原因,我一直在努力解决此问题

这是一个Api URL

这是一个api,您可以在电子邮件中查看响应=digi@gmail.com密码=digi1234

下面是我的堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
这是我的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
}
我使用过的这些库:

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0' 
loginResose.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;
}
}
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;
}
}
@POST("login")
@FormUrlEncoded
Call<LoginResponse> logUser(@Field("email") String email,
                            @Field("password") String password);
 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();
        }
    });
}
ApiService.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;
}
}
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;
}
}
@POST("login")
@FormUrlEncoded
Call<LoginResponse> logUser(@Field("email") String email,
                            @Field("password") String password);
 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();
        }
    });
}
@POST(“登录”)
@FormUrlEncoded
调用logUser(@Field(“email”)字符串email,
@字段(“密码”)字符串(密码);
Login.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;
}
}
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;
}
}
@POST("login")
@FormUrlEncoded
Call<LoginResponse> logUser(@Field("email") String email,
                            @Field("password") String password);
 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();
        }
    });
}
private void go(字符串邮件、字符串密码){
Refundation-Refundation=RefundationClient.getInstance();
ApiService ApiService=reformation.create(ApiService.class);
apiService.logUser(mail,pwd).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
prg.discouse();
List res=response.body().getData();
Toast.makeText(getApplicationContext(),res.get(0).getEmail(),Toast.LENGTH_SHORT).show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
prg.discouse();
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
有人请让我知道为什么我从服务器收到空响应或我做错了什么。任何帮助都将不胜感激


谢谢首先检查response.body()是否为空。然后检查数据内部响应是否为null或[]。然后,如果这些要求都满足了,那么就拿下清单,展示祝酒词。

你也应该分享基本URL。这将有助于任何试图为您排除故障的人。我已发布了URL,请检查它。
response.body()
正在显示响应。请尝试检查您是否正在“数据”中获取数据您的响应中的字段啊
data
数组中有数据。我已经在我的帖子中添加了api url和登录凭据,请检查。您的api正在返回以下响应{“状态”:false,“数据”:“电子邮件和密码不正确。请重试?”,“代码”:400}