Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for the result on a Okhttp call to use it on a test?

I've created a method to check if my app is able to connect to my server using OkHttp.

This is my test class:

public class NetworkTest {

static boolean resultWeb = false;

public static boolean pingTestWeb() {

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url("http://www.google.com")//My server address will go here
            .build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
            resultWeb = false;
            Log.i("Error","Failed to connect: "+e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {

            Log.i("Success","Success: "+response.code());
            if (response.code() == 200) {
                resulWeb = true;
            }
        }
    });

    return resultWeb;
}

And here is where I'm making the test on my activity on OnCreate():

if (NetworkTest.pingTestWeb()) {

        // Do something if true...

    } else {

        // Do something if false, like showing an AlertDialog...
    }

The question is, my pingTestWeb have the default Timeout time of 10000ms, how do I make the activity create the AlertDialog only if the pingTestWeb is false? Because it isn't waiting for the response.

like image 553
Rafael Avatar asked Jan 04 '16 18:01

Rafael


People also ask

How do you test for OkHttp?

The recommended way of testing your code that uses OkHttp is to utilise their MockWebServer utility. This allows the execution of tests in a realistic operation with full control of responses being passed to the client.

How do I use OkHttp client?

Using OkHttp for efficient network access It supports the SPDY protocol. SPDY is the basis for HTTP 2.0 and allows multiple HTTP requests to be multiplexed over one socket connection. If you are using Maven or Gradle as build system you can simply add a dependency to group ID com. squareup.

Is OkHttp blocked?

OkHttp Overview At a high level, the client is designed for both blocking synchronous calls and nonblocking asynchronous calls.


1 Answers

You can also use CountDownLatch, in this way you can put async in your test:

public class NetworkTest {

static boolean resultWeb = false;

public static boolean pingTestWeb() {

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url("http://www.google.com")//My server address will go here
            .build();

    CountDownLatch countDownLatch = new CountDownLatch(1);
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
            resultWeb = false;
            Log.i("Error","Failed to connect: "+e.getMessage());
            countDownLatch.countDown();
        }

        @Override
        public void onResponse(Response response) throws IOException {

            Log.i("Success","Success: "+response.code());
            if (response.code() == 200) {
                resulWeb = true;
            }
            countDownLatch.countDown();
        }
    });

    countDownLatch.await();
    return resultWeb;
}
like image 118
foxwendy Avatar answered Oct 11 '22 12:10

foxwendy