Search in sources :

Example 1 with Callback

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();
            }
        }
    });
}
Also used : Response(okhttp3.Response) RequestCall(com.zhy.http.okhttp.request.RequestCall) Call(okhttp3.Call) Callback(com.zhy.http.okhttp.callback.Callback) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

Callback (com.zhy.http.okhttp.callback.Callback)1 RequestCall (com.zhy.http.okhttp.request.RequestCall)1 IOException (java.io.IOException)1 Call (okhttp3.Call)1 Response (okhttp3.Response)1