【问题标题】:How to use a service of mode synchronous in android with loopj如何在带有loopj的android中使用模式同步的服务
【发布时间】:2015-08-20 16:22:54
【问题描述】:
早安,
此刻,我正在开发一个使用异步模式网络服务的应用程序,但我遇到了一些问题。
我需要使用模式 同步 的网络服务来继续我的主线程没有任何问题。
非常感谢!!!
【问题讨论】:
标签:
android
web-services
asynchronous
synchronous
loopj
【解决方案1】:
与我们对 loopj 使用 async-http-client 的方式相同,但它会阻塞 UI 直到完成。
SyncHttpClient syncHttpClient = new SyncHttpClient();
syncHttpClient.setTimeout(timeout);
syncHttpClient.post(url, params, responseHandler);
syncHttpClient .post("http://example.com", params, new JsonHttpResponseHandler() {
@Override
public void onStart() {
// you can do something here before request starts
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// success logic here
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject errorResponse) {
// handle failure here
}
});