Skip to content

Commit

Permalink
修改为先缓存,在回调成功,默认使用不缓存模式
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Sep 9, 2016
1 parent 1799b3a commit 18bd702
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@
.setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS) //全局的读取超时时间
.setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS) //全局的写入超时时间

//可以全局统一设置缓存模式,默认就是Default,可以不传,具体其他模式看 github 介绍 https://github.com/jeasonlzy0216/
.setCacheMode(CacheMode.DEFAULT)
//可以全局统一设置缓存模式,默认是不使用缓存,可以不传,具体其他模式看 github 介绍 https://github.com/jeasonlzy0216/
.setCacheMode(CacheMode.NO_CACHE)

//可以全局统一设置缓存时间,默认永不过期,具体使用方法看 github 介绍
.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lzy/okhttpdemo/GApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public void onCreate() {
.setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS) //全局的读取超时时间
.setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS) //全局的写入超时时间

//可以全局统一设置缓存模式,默认就是Default,可以不传,具体其他模式看 github 介绍 https://github.com/jeasonlzy0216/
.setCacheMode(CacheMode.DEFAULT)
//可以全局统一设置缓存模式,默认是不使用缓存,可以不传,具体其他模式看 github 介绍 https://github.com/jeasonlzy0216/
.setCacheMode(CacheMode.NO_CACHE)

//可以全局统一设置缓存时间,默认永不过期,具体使用方法看 github 介绍
.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.lzy.okhttpdemo.R;
import com.lzy.okhttpdemo.base.BaseActivity;
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public <T> void execute(AbsCallback<T> callback) {

//请求之前获取缓存信息,添加缓存头和其他的公共头
if (cacheKey == null) cacheKey = HttpUtils.createUrlFromParams(baseUrl, params.urlParamsMap);
if (cacheMode == null) cacheMode = CacheMode.DEFAULT;
if (cacheMode == null) cacheMode = CacheMode.NO_CACHE;
//无缓存模式,不需要进入缓存逻辑
CacheEntity<T> cacheEntity = null;
if (cacheMode != CacheMode.NO_CACHE) {
Expand Down Expand Up @@ -472,9 +472,10 @@ public void onResponse(Call call, Response response) throws IOException {

try {
T data = (T) mCallback.parseNetworkResponse(response);
sendSuccessResultCallback(false, data, call, response, mCallback);
//网络请求成功,保存缓存数据
handleCache(response.headers(), data);
//网络请求成功回调
sendSuccessResultCallback(false, data, call, response, mCallback);
} catch (Exception e) {
//一般为服务器响应成功,但是数据解析错误
sendFailResultCallback(false, call, response, e, mCallback);
Expand Down

0 comments on commit 18bd702

Please sign in to comment.