use of com.zhy.http.okhttp.callback.Callback in project okhttputils by hongyangAndroid.
the class OkHttpUtils method execute.
public void execute(final RequestCall requestCall, Callback callback) {
if (callback == null)
callback = Callback.CALLBACK_DEFAULT;
final Callback finalCallback = callback;
final int id = requestCall.getOkHttpRequest().getId();
requestCall.getCall().enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, final IOException e) {
sendFailResultCallback(call, e, finalCallback, id);
}
@Override
public void onResponse(final Call call, final Response response) {
try {
if (call.isCanceled()) {
sendFailResultCallback(call, new IOException("Canceled!"), finalCallback, id);
return;
}
if (!finalCallback.validateReponse(response, id)) {
sendFailResultCallback(call, new IOException("request failed , reponse's code is : " + response.code()), finalCallback, id);
return;
}
Object o = finalCallback.parseNetworkResponse(response, id);
sendSuccessResultCallback(o, finalCallback, id);
} catch (Exception e) {
sendFailResultCallback(call, e, finalCallback, id);
} finally {
if (response.body() != null)
response.body().close();
}
}
});
}
Aggregations