Reputation: 281
I'm making a GET request:
public interface CheckUserInDBRequest {
@GET("api/checkUserInDB.php")
Call<ResponseBody> searchForUser(
@Query("login") String login,
@Query("pass") String pass
);
}
And I get the answer true || false in json, according to whether there is a user in the database or not.
Retrofit.Builder builder = new Retrofit.Builder().baseUrl("https://kurusa.zhecky.net/").addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
CheckUserInDBRequest client = retrofit.create(CheckUserInDBRequest.class);
Call<ResponseBody> call = client.searchForUser (
UserLogin.getText().toString(),
UserPass.getText().toString()
);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
Toast.makeText(MainActivity.this, response.body().toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
Toast.makeText(MainActivity.this, "no ", Toast.LENGTH_SHORT).show();
}
});
Here is just the output of okHttp3
. I do not understand how to get a normal answer.
Upvotes: 0
Views: 45
Reputation: 44901
First, create a class representing your JSON
object
public class UserResult {
public boolean isSet;
}
Restructure your Retrofit
call in this way
@GET("api/checkUserInDB.php")
Call<UserResult> searchForUser(
@Query("login") String login,
@Query("pass") String pass
);
Then in your code:
call.enqueue(new Callback<UserResult>() {
@Override
public void onResponse(@NonNull Call<UserResult> call, @NonNull Response<UserResult> response) {
if (response.isSuccesfull()) {
UserResult result = response.body();
//use the value result.isSet where you need it
} else {
//something is broken
}
}
@Override
public void onFailure(@NonNull Call<UserResult> call, @NonNull Throwable t) {
Toast.makeText(MainActivity.this, "no ", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 2