Search in sources :

Example 1 with Callback

use of com.okhttplib.callback.Callback in project OkHttp3 by MrZhousf.

the class MainActivity method networkThenCache.

/**
 * 先网络再缓存:先请求网络,失败则请求缓存
 */
private void networkThenCache() {
    OkHttpUtil.Builder().setCacheType(CacheType.NETWORK_THEN_CACHE).build(this).doGetAsync(HttpInfo.Builder().setUrl(url).build(), new Callback() {

        @Override
        public void onSuccess(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("NETWORK_THEN_CACHE:" + result);
            setFromCacheTV(info);
        }

        @Override
        public void onFailure(HttpInfo info) throws IOException {
            resultTV.setText("NETWORK_THEN_CACHE:" + info.getRetDetail());
        }
    });
    needDeleteCache(true);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Callback(com.okhttplib.callback.Callback) IOException(java.io.IOException)

Example 2 with Callback

use of com.okhttplib.callback.Callback in project OkHttp3 by MrZhousf.

the class MainActivity method async.

/**
 * 异步请求:回调方法可以直接操作UI
 */
private void async() {
    doHttpAsync(HttpInfo.Builder().setUrl(url).setRequestType(// 设置请求方式
    RequestType.GET).addHead("head", // 添加头参数
    "test").addParam("param", // 添加接口参数
    "test").setDelayExec(2, // 延迟2秒执行
    TimeUnit.SECONDS).build(), new Callback() {

        @Override
        public void onFailure(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("异步请求失败:" + result);
        }

        @Override
        public void onSuccess(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("异步请求成功:" + result);
            // GSon解析
            TimeAndDate time = info.getRetDetail(TimeAndDate.class);
            LogUtil.d("MainActivity", time.getResult().toString());
            setFromCacheTV(info);
        }
    });
    needDeleteCache(true);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Callback(com.okhttplib.callback.Callback) TimeAndDate(com.okhttp3.bean.TimeAndDate) IOException(java.io.IOException)

Example 3 with Callback

use of com.okhttplib.callback.Callback in project OkHttp3 by MrZhousf.

the class MainActivity method tenSecondCache.

/**
 * 缓存10秒失效:连续点击进行测试10秒内再次请求为缓存响应,10秒后再请求则缓存失效并进行网络请求
 */
private void tenSecondCache() {
    // 由于采用同一个url测试,需要先清理缓存
    if (isNeedDeleteCache) {
        isNeedDeleteCache = false;
        OkHttpUtil.getDefault().deleteCache();
    }
    OkHttpUtil.Builder().setCacheType(CacheType.CACHE_THEN_NETWORK).setCacheSurvivalTime(// 缓存存活时间为10秒
    10).build(this).doGetAsync(HttpInfo.Builder().setUrl(url).build(), new Callback() {

        @Override
        public void onSuccess(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("缓存10秒失效:" + result);
            setFromCacheTV(info);
        }

        @Override
        public void onFailure(HttpInfo info) throws IOException {
            resultTV.setText("缓存10秒失效:" + info.getRetDetail());
        }
    });
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Callback(com.okhttplib.callback.Callback) IOException(java.io.IOException)

Example 4 with Callback

use of com.okhttplib.callback.Callback in project OkHttp3 by MrZhousf.

the class MainActivity method cacheThenNetwork.

/**
 * 先缓存再网络:先请求缓存,失败则请求网络
 */
private void cacheThenNetwork() {
    OkHttpUtil.Builder().setCacheType(CacheType.CACHE_THEN_NETWORK).build(this).doGetAsync(HttpInfo.Builder().setUrl(url).build(), new Callback() {

        @Override
        public void onSuccess(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("CACHE_THEN_NETWORK:" + result);
            setFromCacheTV(info);
        }

        @Override
        public void onFailure(HttpInfo info) throws IOException {
            resultTV.setText("CACHE_THEN_NETWORK:" + info.getRetDetail());
        }
    });
    needDeleteCache(true);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Callback(com.okhttplib.callback.Callback) IOException(java.io.IOException)

Example 5 with Callback

use of com.okhttplib.callback.Callback in project OkHttp3 by MrZhousf.

the class MainActivity method forceCache.

/**
 * 仅缓存请求
 */
private void forceCache() {
    OkHttpUtil.Builder().setCacheType(CacheType.FORCE_CACHE).build(this).doGetAsync(HttpInfo.Builder().setUrl(url).build(), new Callback() {

        @Override
        public void onSuccess(HttpInfo info) throws IOException {
            String result = info.getRetDetail();
            resultTV.setText("FORCE_CACHE:" + result);
            setFromCacheTV(info);
        }

        @Override
        public void onFailure(HttpInfo info) throws IOException {
            resultTV.setText("FORCE_CACHE:" + info.getRetDetail());
        }
    });
    needDeleteCache(true);
}
Also used : HttpInfo(com.okhttplib.HttpInfo) Callback(com.okhttplib.callback.Callback) IOException(java.io.IOException)

Aggregations

HttpInfo (com.okhttplib.HttpInfo)7 Callback (com.okhttplib.callback.Callback)7 IOException (java.io.IOException)7 TimeAndDate (com.okhttp3.bean.TimeAndDate)1