Okhttp在主线程上的响应回调 [英] Okhttp response callbacks on the main thread
问题描述
我创建了一个帮助程序类来处理我的应用程序中的所有http调用.这是okhttp的一个简单的单例包装,看起来像这样(我省略了一些不重要的部分):
I have created a helper class to handle all of my http calls in my app. It is a simple singleton wrapper for okhttp that looks like this (I have omitted some unimportant parts):
public class HttpUtil {
private OkHttpClient client;
private Request.Builder builder;
...
public void get(String url, HttpCallback cb) {
call("GET", url, cb);
}
public void post(String url, HttpCallback cb) {
call("POST", url, cb);
}
private void call(String method, String url, final HttpCallback cb) {
Request request = builder.url(url).method(method, method.equals("GET") ? null : new RequestBody() {
// don't care much about request body
@Override
public MediaType contentType() {
return null;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
}
}).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, Throwable throwable) {
cb.onFailure(null, throwable);
}
@Override
public void onResponse(Response response) throws IOException {
if (!response.isSuccessful()) {
cb.onFailure(response, null);
return;
}
cb.onSuccess(response);
}
});
}
public interface HttpCallback {
/**
* called when the server response was not 2xx or when an exception was thrown in the process
* @param response - in case of server error (4xx, 5xx) this contains the server response
* in case of IO exception this is null
* @param throwable - contains the exception. in case of server error (4xx, 5xx) this is null
*/
public void onFailure(Response response, Throwable throwable);
/**
* contains the server response
* @param response
*/
public void onSuccess(Response response);
}
}
然后,在我的主要活动中,我使用以下帮助程序类:
Then, in my main activity, I use this helper class :
HttpUtil.get(url, new HttpUtil.HttpCallback() {
@Override
public void onFailure(Response response, Throwable throwable) {
// handle failure
}
@Override
public void onSuccess(Response response) {
// <-------- Do some view manipulation here
}
});
onSuccess
在代码运行时引发异常:
onSuccess
throws an exception when the code runs :
android.view.ViewRootImpl $ CalledFromWrongThreadException:仅 创建视图层次结构的原始线程可以触摸其视图.
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
据我了解,Okhttp回调在主线程上运行,所以为什么会出现此错误?
From my understanding, Okhttp callbacks run on the main thread so why do I get this error ?
**作为一个旁注,我创建了HttpCallback
接口来包装Okhttp的Callback
类,因为我想更改onResponse
和onFailure
的行为,因此我可以统一处理失败响应的逻辑由于I/O异常以及由于服务器问题而导致响应失败.
** Just as a side note, I have created HttpCallback
interface to wrap Okhttp's Callback
class because I wanted to change the behaviour of onResponse
and onFailure
so I could unite the logic of handling failed responses due to i/o exception and failed responses due to server problems.
谢谢.
推荐答案
据我了解,Okhttp回调在主线程上运行,那么为什么会出现此错误?
From my understanding, Okhttp callbacks run on the main thread so why do I get this error ?
这不是事实.回调在后台线程上运行.如果要立即在UI中处理某些内容,则需要发布到主线程中.
This is not true. Callbacks run on a background thread. If you want to immediately process something in the UI you will need to post to the main thread.
由于您已经在回调周围包含了包装器,因此可以在帮助器内部进行此操作,以便为方便起见在主线程上调用所有HttpCallback
方法.
Since you already have a wrapper around the callback you can do this internally in your helper so that all HttpCallback
methods are invoked on the main thread for convenience.
这篇关于Okhttp在主线程上的响应回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!