我无法从API得到任何响应。我正在使用翻新库进行联网。这是我的代码
LoginApi loginApi = APIClient.getApiClient().create(LoginApi.class);
Call<Login> call = loginApi.loginApiCall("jkchoudhary121@gmail.com", "123456");
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(@NonNull Call<Login> call, @NonNull Response<Login> response) {
if (response.isSuccessful()){
Log.i("Response", response.toString());
}
else {
Log.i("Error", "Wrong");
}
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
Log.i("Error", t.getMessage());
}
});
我尝试用两种方式定义改造实例,但仍然没有得到响应。
这是我的Web服务。
public class APIClient {
private static final String base_url = "https://myUrl/api/v1/users/";
private static Retrofit retrofit = null;
// private static Gson gson = new GsonBuilder().setLenient().create();
private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(7, TimeUnit.MINUTES)
.readTimeout(7, TimeUnit.MINUTES)
.writeTimeout(7, TimeUnit.MINUTES)
.build();
//
public static Retrofit getApiClient() {
if (retrofit == null) {
//scalar is for text and gson for json obect and arrays
retrofit = new Retrofit.Builder().baseUrl(base_url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
该呼叫进入onReponse,但未返回任何内容。这是我的Login APi界面。
public interface LoginApi {
//login
@FormUrlEncoded
@POST("login")
Call<Login> loginApiCall(@Field("email") String email,
@Field("password") String password);
//signup
@FormUrlEncoded
@POST("register")
Call<JSONObject> signupApiCall(@Field("email") String email,
@Field("password") String password);
}
这是我的Pojo课程。
public class Login {
@SerializedName("error")
private boolean error;
@SerializedName("message")
private String message;
public Login(boolean error, String message) {
this.error = error;
this.message = message;
}
public boolean getError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我的json响应看起来像这样
"error": "False",
"message": "Logged in successfully",
答案 0 :(得分:0)
您的代码看起来不错。尝试增加超时值。 更改您的代码:
private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.MINUTES)
.readTimeout(100, TimeUnit.MINUTES)
.writeTimeout(100, TimeUnit.MINUTES)
.build();``
`