我正在设置一个新的安卓项目,并使用改进,我的改进功能在模拟器(NOX)和邮递员中工作正常,但当我尝试在移动设备上构建我的应用程序时,更新总是进入onFailure,有人能给我解决方案吗?我的API发布在公共主机上,
这就是我所说的翻新
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();
}
});
}
我的回应
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;
}
}
用户模型
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接口
public interface APIInterface {
@FormUrlEncoded
@POST("login.php")
Call<Response> loginRequest(@Field("username") String username,
@Field("password") String password);
}
我从t.message收到此消息:网络安全策略不允许与{my api url}进行明文通信。
在我将这个添加到清单中之后
android:usesCleartextTraffic="true"
我得到了这个新的'java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第1行第1列路径$处是字符串‘
我的JSON响应是这样的
{"value":1,"result":[{"id":"1","username":"username","password":"password","email":"email","image":null,"point":"0","reputation":"0","role":"2"}]}
这是我的邮递员回复
https://stackoverflow.com/questions/56530457
复制相似问题