Reputation: 3918
I am using Retrofit for the first time, after the login validation I run my retrofit api call function loginApiCall
to verify the user.
I got the response 200 even if my validation are wrong(email and password wrong), but getting the true result of this check response.isSuccessful()
in response.
private void loginApiCall() {
Call<LoginResponse> call = RetrofitClient
.getInstance().getApi().userLogin("*******@gmail.com", "****");
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful())
Toast.makeText(LoginScreen.this, "Success", Toast.LENGTH_SHORT).show();
else
Toast.makeText(LoginScreen.this, "no Success", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(LoginScreen.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
}
Here Retrofit setup for my calls
public class RetrofitClient {
private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient() {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public static synchronized RetrofitClient getInstance() {
if (mInstance == null) {
mInstance = new RetrofitClient();
}
return mInstance;
}
public QareebApiInterface getApi() {
return retrofit.create(QareebApiInterface.class);
}
here is Interface for POST
public interface QareebApiInterface {
@FormUrlEncoded
@POST(WebServiceConstant.END_POINT_NEW_USER)
Call<LoginResponse> userLogin(
@Field("email") String email,
@Field("password") String password
);
}
Here is gradle files related to retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
I have converted volley call to retrofit where only username and password was sending to the server (There is no Auth). Is there any issue of auth as i am not sending or missing anything? but if missing why response is 200?
Following is the volley call(which is working fine) which i replaced with retrofit.
public void loginApiCall(final String email, final String password) {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest postRequest = new StringRequest(Request.Method.POST, BASE_URL,
new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: "+response);
}
},
new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
return params;
}
};
queue.add(postRequest);
}
Upvotes: 0
Views: 2138
Reputation: 11
I've the same issue and on okhttp I see my JSON response. It seems an issue on GSON parsing but can't fix it.
On debug the response is populated except that the body
Upvotes: 0
Reputation: 6265
You need to add logging to see real response
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new
OkHttpClient.Builder().addInterceptor(interceptor).build();
It's can be 200 with error description
Upvotes: 0