如何等待 Okhttp 调用的结果以在测试中使用它?
How to wait for the result on a Okhttp call to use it on a test?
我创建了一种方法来检查我的应用程序是否能够使用 OkHttp 连接到我的服务器。
这是我的测试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;
}
这是我在 OnCreate() 上对 activity 进行测试的地方:
if (NetworkTest.pingTestWeb()) {
// Do something if true...
} else {
// Do something if false, like showing an AlertDialog...
}
问题是,我的 pingTestWeb 的默认超时时间为 10000 毫秒,如何让 activity 只有在 pingTestWeb 为 false 时才创建 AlertDialog?因为它不等待响应。
我认为这里可能发生的情况是您正在异步执行 OkHttp 调用,因此您在任务完成之前点击了 return 语句。为了测试,是否可以同步调用 OkHttp?您可以使用如下所示的 response.isSuccessful
处理 success/failure 案例。
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;
}
你也可以使用 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;
}
我创建了一种方法来检查我的应用程序是否能够使用 OkHttp 连接到我的服务器。
这是我的测试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;
}
这是我在 OnCreate() 上对 activity 进行测试的地方:
if (NetworkTest.pingTestWeb()) {
// Do something if true...
} else {
// Do something if false, like showing an AlertDialog...
}
问题是,我的 pingTestWeb 的默认超时时间为 10000 毫秒,如何让 activity 只有在 pingTestWeb 为 false 时才创建 AlertDialog?因为它不等待响应。
我认为这里可能发生的情况是您正在异步执行 OkHttp 调用,因此您在任务完成之前点击了 return 语句。为了测试,是否可以同步调用 OkHttp?您可以使用如下所示的 response.isSuccessful
处理 success/failure 案例。
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;
}
你也可以使用 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;
}