我正在建立一个新的android项目并使用改造,我的改造功能可以在模拟器(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}进行CLEARTEXT通信”
将其添加到清单后
android:usesCleartextTraffic="true"
我有了这个新的 'java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第1列的路径$ STRING处>
我的JSON响应就是这样
{"value":1,"result":[{"id":"1","username":"username","password":"password","email":"email","image":null,"point":"0","reputation":"0","role":"2"}]}
这是我的邮递员回复
答案 0 :(得分:0)
使用this网站在Java中生成正确的响应类
您的响应类应为:
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;
}
}
答案 1 :(得分:0)
在项目中尝试使用翻新库时应遵循的事项。
您的BASE URL应该以'/'结尾。
String BASEURL = "http://www.xxxxxxxx.com/"
当实现改造的回调时。您需要通过选中if(response.isSuccessful())
来检查您的回复是否给了您200。
mService.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
if(response.isSuccessful()){
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();
}
}else Toast.makeText(Login.this,response.errorBody().string(),Toast.LENGTH_SHORT).show(); // this will tell you why your api doesnt work most of time
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
Toast.makeText(Login.this,t.getMessage(),Toast.LENGTH_SHORT).show();
}
});
正如我所看到的,您正在使用Response类。响应具有许多预定义的对象,其他库也具有响应的名称。因此,您必须检查导入文件以交叉验证您使用的响应对象是您的包名称,而不是其他响应对象。
与在POSTMAN中一样,您包括9个标头。您必须找出所有9个标头改装示例,并将它们相应地添加到API接口中。
注意:-请遵循以下步骤,我想您会自己解决问题的。
按照上述所有说明操作后,您可以随意提问。
答案 2 :(得分:-1)
您设置了正确的IP地址吗?
如果在邮递员和仿真器中正常运行,则您的代码运行良好。但是,请确保设置正确的终结点(或您的情况下的BASE_URL)
OR
如果您使用的是ProGuard,请确保禁用它只是为了检查没有proguard的情况下一切正常。