黄舟2017-06-26 10:51:532楼

当然要用CountDownLatch 啦,异步请求转阻塞式同步请求

public String getPhone(String id) {

String url = "http://www.163.net/";

final CountDownLatch latch = new CountDownLatch(1);

final String[] phone = new String[1];

OkHttpUtils

.get()

.url(url)

.addParams("username", "abc")

.addParams("password", "123")

.build()

.execute(new StringCallback() {

@Override

public void onError(Call call, Exception e, int id) {

latch.countDown();

}

@Override

public void onResponse(String response, int id) {

phone[0] = response;

latch.countDown();

}

});

try {

latch.await();

} catch (InterruptedException e) {

}

return phone[0];

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐