I am learning Retrofit. I've built an API using PHP Slim to check whether an email given by user exist or not in my database. My API is okay. I've tested it via Postman. But when I'm calling my API it's not giving correct result.
Api.java
package com.example.royta.retrofitdemo.APIs;
import com.example.royta.retrofitdemo.ModelClass.UserExist;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface Api {
@GET("finduser")
Call<UserExist> isUserExist(
@Query("email") String email
);
}
Please check the Screenshot for JSON response.
UserExist.java
package com.example.royta.retrofitdemo.ModelClass;
import com.google.gson.annotations.SerializedName;
public class UserExist {
@SerializedName("email")
private boolean email;
public UserExist(boolean email) {
this.email = email;
}
public boolean isEmailExist() {
return email;
}
}
RetrofitClient.java
package com.example.royta.retrofitdemo.APIs;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static final String BASE_URL = "http://24d00bb3.ngrok.io/MyApi/public/";
private static RetrofitClient retrofitClient;
private Retrofit retrofit;
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getInstance() {
if(retrofitClient == null) {
retrofitClient = new RetrofitClient();
}
return retrofitClient;
}
public Api getApi() {
return retrofit.create(Api.class);
}
}
Here is the call
FindAccount.java
Call <UserExist> call = RetrofitClient.getInstance().getApi().isUserExist("troy@yahoo.com");
call.enqueue(new Callback<UserExist>() {
@Override
public void onResponse(Call<UserExist> call, Response<UserExist> response) {
if(response.body().isEmailExist()) {
//send code in email;
Toast.makeText(FindAccount.this, "Send code in email", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(FindAccount.this, "You are not registered", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<UserExist> call, Throwable t) {
Log.d("ROYs", "onFailure block");
}
});
the email "troy@yahoo.com" is exist in my database.But the following call giving me false result. It should be true.
response.body().isEmailExist()
Screenshot is the proof of my API. Please Help. Thanks in advance.