K
K
Kurusa2018-05-18 23:34:06
Java
Kurusa, 2018-05-18 23:34:06

How to get a response from Retrofit?

I am 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 in response true || false in json, depending on whether the user is 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();
    }
});

It just outputs okHttp3. I do not understand how to get a normal answer.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Perelygin, 2018-05-28
@orcDamnar

Well, with the help of ConverterFacetory, it should immediately generate an object from JSON. for example if object {result:true} -

UserInDatabaseStatus{ 
     private boolean result; 
     public boolean getResult(){
          return result;
     } 
     public void setResult(boolean result){
          this.result = result
     }

and then :
@GET("api/checkUserInDB.php")
    Call<UserInDatabaseStatus> searchForUser(
            @Query("login") String login,
            @Query("pass") String pass

G
Gregary, 2020-03-02
@Gregary

Here is an excellent tutorial in Russian and Kotlin on how to work with GSON and Retrofit androidschool.ru/courses/android-retrofit-and-gson

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question