Я настраиваю новый проект 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 'Связь CLEARTEXT с {мой api url} не разрешена политикой безопасности сети'
После добавления этого в манифест
android:usesCleartextTraffic="true"
Я получил этот новый 'java.lang.IllegalStateException: ожидаемый BEGIN_OBJECT, но он был STRING в строке 1, путь 1 столбца $'
Мой JSON-ответ похож на
{"value":1,"result":[{"id":"1","username":"username","password":"password","email":"email","image":null,"point":"0","reputation":"0","role":"2"}]}
Это мой ответ почтальона
![enter image description here](https://i.stack.imgur.com/3Xtpu.png)