首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >改装始终响应onFailure

改装始终响应onFailure
EN

Stack Overflow用户
提问于 2019-06-11 00:52:34
回答 1查看 518关注 0票数 0

我正在设置一个新的安卓项目,并使用改进,我的改进功能在模拟器(NOX)和邮递员中工作正常,但当我尝试在移动设备上构建我的应用程序时,更新总是进入onFailure,有人能给我解决方案吗?我的API发布在公共主机上,

这就是我所说的翻新

代码语言:javascript
复制
 private APIInterface getInterfaceService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        final APIInterface mInterfaceService = retrofit.create(APIInterface.class);
        return mInterfaceService;
    }

 private void loginInterface(final String username, final String password){
        APIInterface mApiService = this.getInterfaceService();
        Call<Response> mService = mApiService.loginRequest(username,password);
        mService.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                if(response.body().getValue()==1){
                    Toast.makeText(Login.this,"Welcome",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(),HomePage.class);
                    startActivity(intent);
                    finish();
                }else{
                    Toast.makeText(Login.this,"Invalid Username or Password",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Response> call, Throwable t) {
                Toast.makeText(Login.this,t.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }

我的回应

代码语言:javascript
复制
 public class Response {

    @SerializedName("value")
    @Expose
    private Integer value;
    @SerializedName("result")
    @Expose
    private List<User> result = null;

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public List<User> getResult() {
        return result;
    }

    public void setResult(List<User> result) {
        this.result = result;
    }

}

用户模型

代码语言:javascript
复制
 public class User {
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("password")
    @Expose
    private String password;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("image")
    @Expose
    private Object image;
    @SerializedName("point")
    @Expose
    private String point;
    @SerializedName("reputation")
    @Expose
    private String reputation;
    @SerializedName("role")
    @Expose
    private String role;

    public User(String id, String username, String password, String email, Object image, String point, String reputation, String role) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.image = image;
        this.point = point;
        this.reputation = reputation;
        this.role = role;
    }

    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 getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

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

    public Object getImage() {
        return image;
    }

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

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public String getReputation() {
        return reputation;
    }

    public void setReputation(String reputation) {
        this.reputation = reputation;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

API接口

代码语言:javascript
复制
public interface APIInterface {
    @FormUrlEncoded
    @POST("login.php")
    Call<Response> loginRequest(@Field("username") String username,
                            @Field("password") String password);
}

我从t.message收到此消息:网络安全策略不允许与{my api url}进行明文通信。

在我将这个添加到清单中之后

代码语言:javascript
复制
android:usesCleartextTraffic="true"

我得到了这个新的'java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第1行第1列路径$处是字符串‘

我的JSON响应是这样的

代码语言:javascript
复制
{"value":1,"result":[{"id":"1","username":"username","password":"password","email":"email","image":null,"point":"0","reputation":"0","role":"2"}]}

这是我的邮递员回复

EN

回答 1

Stack Overflow用户

发布于 2019-06-11 01:44:33

使用this站点在java中生成正确的响应类

您的响应类应该如下所示:

代码语言:javascript
复制
public class Response implements Serializable
{

@SerializedName("value")
@Expose
private long value;
@SerializedName("result")
@Expose
private List<Result> result = null;
private final static long serialVersionUID = -7121130042760098410L;

public long getValue() {
return value;
}

public void setValue(long value) {
this.value = value;
}

public List<Result> getResult() {
return result;
}

public void setResult(List<Result> result) {
this.result = result;
}

}
-----------------------------------com.example.Result.java-----------------------------------

package com.example;

import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result implements Serializable
{

@SerializedName("id")
@Expose
private String id;
@SerializedName("username")
@Expose
private String username;
@SerializedName("password")
@Expose
private String password;
@SerializedName("email")
@Expose
private String email;
@SerializedName("image")
@Expose
private Object image;
@SerializedName("point")
@Expose
private String point;
@SerializedName("reputation")
@Expose
private String reputation;
@SerializedName("role")
@Expose
private String role;
private final static long serialVersionUID = 7267197789545166983L;

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 getPassword() {
return password;
}

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

public String getEmail() {
return email;
}

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

public Object getImage() {
return image;
}

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

public String getPoint() {
return point;
}

public void setPoint(String point) {
this.point = point;
}

public String getReputation() {
return reputation;
}

public void setReputation(String reputation) {
this.reputation = reputation;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56530457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档