Search in sources :

Example 1 with IHttpCallback

use of com.qiyei.sdk.http.base.IHttpCallback in project EssayJoke by qiyei2015.

the class HttpManager method execute.

/**
 * 执行http请求,并返回响应的task id
 * @param fragmentManager
 * @param request
 * @param callback
 * @return
 */
@Override
public <T> String execute(FragmentManager fragmentManager, HttpRequest request, final INetCallback<T> callback) {
    if (request == null) {
        return null;
    }
    mFragmentManager = fragmentManager;
    tag = request.getUrl();
    // 显示加载对话框
    showDialog();
    // 是否缓存
    final boolean cache = request.isUseCache();
    String url = HttpUtil.jointParams(request.getUrl(), request.getParams());
    final String urlKey = MD5Util.string2MD5(url);
    mHttpCallback = new IHttpCallback() {

        @Override
        public void onSuccess(String result) {
            if (cache) {
                String cache = HttpUtil.getCache(urlKey);
                if (cache != null && cache.equals(result)) {
                    // 数据一致,不用刷新界面
                    LogManager.d(TAG, "数据一致,不用刷新界面");
                    // 关闭加载对话框
                    dismissDialog();
                    return;
                }
            }
            final T obj = (T) HttpUtil.parseJson(result, callback.getClass());
            if (cache) {
                // 2.3 缓存数据
                long num = HttpUtil.setCache(urlKey, result);
                LogManager.d(TAG, "num --> " + num);
            }
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    // 关闭加载对话框
                    dismissDialog();
                    if (cache) {
                        LogManager.d(TAG, "数据不一致,刷新界面");
                    }
                    callback.onSuccess(obj);
                }
            });
        }

        @Override
        public void onFail(final Exception e) {
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    // 关闭加载对话框
                    dismissDialog();
                    callback.onFail(e);
                }
            });
        }
    };
    if (cache) {
        String cacheResult = HttpUtil.getCache(urlKey);
        // 有缓存
        if (!TextUtils.isEmpty(cacheResult)) {
            T obj = (T) HttpUtil.parseJson(cacheResult, callback.getClass());
            // 关闭加载对话框
            dismissDialog();
            callback.onSuccess(obj);
            LogManager.d(TAG, "有缓存,使用缓存刷新界面");
            return "222";
        // TODO: 2017/6/5 后续再改
        }
    }
    switch(request.getRequestMethod()) {
        case GET:
            mEngine.get(request, mHttpCallback);
            break;
        case POST:
            mEngine.post(request, mHttpCallback);
            break;
        default:
            break;
    }
    return null;
}
Also used : IHttpCallback(com.qiyei.sdk.http.base.IHttpCallback)

Example 2 with IHttpCallback

use of com.qiyei.sdk.http.base.IHttpCallback in project EssayJoke by qiyei2015.

the class OkHttpEngine method post.

@Override
public void post(HttpRequest httpRequest, final IHttpCallback callback) {
    RequestBody requestBody = buildPostRequest(httpRequest);
    Request request = new Request.Builder().url(httpRequest.getUrl()).post(requestBody).build();
    LogManager.e("Post请求路径:", httpRequest.getUrl());
    mClient.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            callback.onFail(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response != null && response.isSuccessful()) {
                callback.onSuccess(response.body().string());
            } else {
                callback.onFail(new Exception("error"));
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) IHttpCallback(com.qiyei.sdk.http.base.IHttpCallback) Request(okhttp3.Request) HttpRequest(com.qiyei.sdk.http.base.HttpRequest) IOException(java.io.IOException) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 3 with IHttpCallback

use of com.qiyei.sdk.http.base.IHttpCallback in project EssayJoke by qiyei2015.

the class OkHttpEngine method get.

@Override
public void get(HttpRequest req, final IHttpCallback callback) {
    String url = HttpHelper.buildGetRequest(req);
    LogManager.e("Get请求路径:", url);
    okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(url);
    Request request = builder.build();
    mClient.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            callback.onFail(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response != null && response.isSuccessful()) {
                callback.onSuccess(response.body().string());
            } else {
                callback.onFail(new Exception("error"));
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) IHttpCallback(com.qiyei.sdk.http.base.IHttpCallback) Request(okhttp3.Request) HttpRequest(com.qiyei.sdk.http.base.HttpRequest) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

IHttpCallback (com.qiyei.sdk.http.base.IHttpCallback)3 HttpRequest (com.qiyei.sdk.http.base.HttpRequest)2 IOException (java.io.IOException)2 Call (okhttp3.Call)2 Callback (okhttp3.Callback)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 RequestBody (okhttp3.RequestBody)1