0

I have an HTTP request inside of a method and i'm trying to return true/false based on the response from the request. im not sure what im doing wrong, I thought it was pretty straight forward. in the test scenario the condition inside of the onResponse call returns true. not sure whats getting lost in translation. Also, in the final condition, the "message" variable is purple, in android studio, im not sure what this indicates. I think its related to my issue. suggestions?

public class DeleteButtonChecker {
    public String message = new String();
    public Boolean doCheck(int userID, int postID){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        RequestInterface requestInterface = retrofit.create(RequestInterface.class);
        Call<ServerResponse> response = requestInterface.check(postID, userID);
        response.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                ServerResponse resp = response.body();
                if(resp.getResult().equals(Constants.SUCCESS)){
                    message = "true";
                } else {
                    message = "false";
                }
            }
            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {

            }
        });
        if(message.equals("true")) {
            return true;
        } else{
            return false;
        }
    }
}
  • 1
    What happens if you debug your code? Track the actual vs. expected values for your key variables line by line until you find the bug. Hint: you probably don't want to make message public, as it is is a private property that holds object state. Also, putting true/false state in a String is crufty. Why don't you use a boolean? Your use of `new String()` is not necessary. See here: https://stackoverflow.com/q/3652369/1531971 –  Feb 06 '18 at 18:33
  • 5
    Your http request executes asynchronously and your function will return before you actually receive response from server. You cannot have function returning value but you have to implement callback that will execute after you get server response. – Dalija Prasnikar Feb 06 '18 at 18:35
  • 1
    Is your code is running? How you have set message = "true"/"false" inside the anonymous implementation of the callback. As it should give error like "defined in an enclosing scope must be final or effectively final". – Amit Bera Feb 06 '18 at 18:44
  • Also, unrelated small addition, you should declare your OkHttpClient as a singleton instead of instantiating it every time. –  Feb 06 '18 at 19:04
  • A few suggestions unrelated to your question: 1. Do not use `new String()`. 2. Use boolean values `true` and `false` rather than `String` values `"true"` and `"false"`. – Code-Apprentice Feb 06 '18 at 19:50

2 Answers2

2

The response is handled asynchronously. On the line:

response.enqueue(new Callback<ServerResponse>() {
...

You are simply giving a callback to be executed once the response is returned. However the method is not blocked and continues to the next statement - which is the:

if(message.equals("true")) {

Which can be translated to:

if("".equals("true")) {
Nas
  • 1,063
  • 2
  • 9
  • 22
0

An asynchronous request cannot be treated as a synchronous call, that is given your conditions. If it was for me I would use RxJava 2 Single with Retrofit to return a usable result from the request.

Check this out: https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2

And make sure to learn some RxJava which can help you on the road

Seaskyways
  • 3,630
  • 3
  • 26
  • 43