【问题标题】:How to wait for the result on a Okhttp call to use it on a test?如何等待 Okhttp 调用的结果以在测试中使用它?
【发布时间】:2016-04-08 10:10:30
【问题描述】:

我创建了一个方法来检查我的应用是否能够使用 OkHttp 连接到我的服务器。

这是我的测试课:

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;
}

这是我在 OnCreate() 上对我的活动进行测试的地方:

if (NetworkTest.pingTestWeb()) {

        // Do something if true...

    } else {

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

问题是,我的 pingTestWeb 的默认超时时间为 10000 毫秒,如何让活动仅在 pingTestWeb 为 false 时创建 AlertDialog?因为它不等待响应。

【问题讨论】:

    标签: android connection timeout ping okhttp


    【解决方案1】:

    你也可以使用 CountDownLatch,这样你就可以在你的测试中加入异步:

    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;
    }
    

    【讨论】:

    • 我认为这是一个更好的答案,因为它不需要为了测试而更改代码。
    【解决方案2】:

    我认为这里可能发生的情况是您正在异步执行 OkHttp 调用,因此您在任务完成之前点击了 return 语句。为了测试,是否可以同步进行 OkHttp 调用?您可以使用response.isSuccessful 处理成功/失败情况,如下所示。

       private final OkHttpClient client = new OkHttpClient();
    
       public void run() throws Exception {
           Request request = new Request.Builder()
               .url("your_url_here")
               .build();
    
           Response response = client.newCall(request).execute();
           if(response.isSuccessful()){
               return true;             
            }else return false;
    
        }
    

    【讨论】:

    • 刚刚注意到您在 onCreate 中调用了这个测试。可能最好让它保持异步。您可以将各自的返回语句放在 onFailure 和 onResponse 方法中。
    • 好的,我明白了,我的 pingTestWeb 在另一个类中,所以如果我在我的 activityMain 上调用它,它应该等到它从 pingTestWeb 获得返回后才能继续,对吗?或者当我异步执行它时,它会向前移动而不是等待响应?
    • 我相信因为它是异步的,所以它正在向前发展。如果您在这里使用同步方法而不是异步(使用回调),这应该可以工作。
    • 谢谢,我试试。完成后尽快更新。
    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多