Reputation: 147
I am not able to get any response from the API. I am using retrofit library for networking. Here is my code
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());
}
});
I have tried defining retrofit instance in two ways but still I am not getting the response.
Here is my Webservice.
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;
}
}
The call is going into onReponse but it is not returning anything. Here is my Login APi interface.
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);
}
And here is my Pojo class.
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;
}
}
My json response looks like this
"error": "False",
"message": "Logged in successfully",
Upvotes: 0
Views: 51
Reputation: 179
Your code looks fine.Try increasing timeout values. change your code:
private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.MINUTES)
.readTimeout(100, TimeUnit.MINUTES)
.writeTimeout(100, TimeUnit.MINUTES)
.build();``
`
Upvotes: 1