Commit db12e777fb1865bdb5acfa8389fdd9b53fe7407f

Authored by 朱显松
1 parent ebaf1eba

网络缓存接口更新

app/build.gradle
... ... @@ -37,6 +37,9 @@ android {
37 37 dataBinding {
38 38 enabled true
39 39 }
  40 + lintOptions {
  41 + abortOnError false
  42 + }
40 43 }
41 44  
42 45 dependencies {
... ...
app/src/main/java/com/cnlive/libs/demo/data/model/AppVersionInfo.java 0 → 100644
  1 +package com.cnlive.libs.demo.data.model;
  2 +
  3 +public class AppVersionInfo {
  4 +
  5 +
  6 +
  7 + /**
  8 + * deviceType : IOS
  9 + * msg : 达人端1.0
  10 + * createTime : 2018-04-02 00:00:00
  11 + * id : aaaaa
  12 + * type : B
  13 + * version : 1.0
  14 + */
  15 +
  16 + private String deviceType;
  17 + private String msg;
  18 + private String createTime;
  19 + private String id;
  20 + private String type;
  21 + private String version;
  22 + private String versionCode;
  23 +
  24 + public String getVersionCode() {
  25 + return versionCode;
  26 + }
  27 +
  28 + public void setVersionCode(String versionCode) {
  29 + this.versionCode = versionCode;
  30 + }
  31 +
  32 + public String getDownLoadUrl() {
  33 + return downLoadUrl;
  34 + }
  35 +
  36 + public void setDownLoadUrl(String downLoadUrl) {
  37 + this.downLoadUrl = downLoadUrl;
  38 + }
  39 +
  40 + private String downLoadUrl;
  41 +
  42 + public String getDeviceType() {
  43 + return deviceType;
  44 + }
  45 +
  46 + public void setDeviceType(String deviceType) {
  47 + this.deviceType = deviceType;
  48 + }
  49 +
  50 + public String getMsg() {
  51 + return msg;
  52 + }
  53 +
  54 + public void setMsg(String msg) {
  55 + this.msg = msg;
  56 + }
  57 +
  58 + public String getCreateTime() {
  59 + return createTime;
  60 + }
  61 +
  62 + public void setCreateTime(String createTime) {
  63 + this.createTime = createTime;
  64 + }
  65 +
  66 + public String getId() {
  67 + return id;
  68 + }
  69 +
  70 + public void setId(String id) {
  71 + this.id = id;
  72 + }
  73 +
  74 + public String getType() {
  75 + return type;
  76 + }
  77 +
  78 + public void setType(String type) {
  79 + this.type = type;
  80 + }
  81 +
  82 + public String getVersion() {
  83 + return version;
  84 + }
  85 +
  86 + public void setVersion(String version) {
  87 + this.version = version;
  88 + }
  89 +}
0 90 \ No newline at end of file
... ...
app/src/main/java/com/cnlive/libs/demo/data/network/testapi/ApiRequest.java
1 1 package com.cnlive.libs.demo.data.network.testapi;
2 2  
3 3  
  4 +import android.content.Context;
  5 +
4 6 import com.cnlive.libs.base.data.model.IBaseInfo;
5 7 import com.cnlive.libs.base.data.network.RetrofitUtil;
6 8 import com.cnlive.libs.base.data.network.Subscriber;
... ... @@ -33,6 +35,10 @@ public class ApiRequest {
33 35 return RetrofitUtil.getRestAPI(getUrl(clazz), clazz);
34 36 }
35 37  
  38 + public static <T> T service(Class<T> clazz, Context context) {
  39 + return RetrofitUtil.getRestAPI(context, true, true, 10_000, 10_000, getUrl(clazz), clazz);
  40 + }
  41 +
36 42 public static <T extends IBaseInfo> Disposable subscribe(Observable<Result<T>> observable, Subscriber<T> subscriber) {
37 43 return observable.take(1)
38 44 .subscribeOn(Schedulers.io())
... ...
app/src/main/java/com/cnlive/libs/demo/data/network/testapi/ApiService.java
... ... @@ -4,14 +4,27 @@ package com.cnlive.libs.demo.data.network.testapi;
4 4 import com.cnlive.libs.demo.data.model.AppInfo;
5 5 import com.cnlive.libs.demo.mvptest.model.ExpertLibraryListBean;
6 6  
  7 +import java.util.Map;
  8 +
7 9 import io.reactivex.Observable;
8 10 import retrofit2.adapter.rxjava2.Result;
9 11 import retrofit2.http.GET;
  12 +import retrofit2.http.Headers;
10 13 import retrofit2.http.Query;
  14 +import retrofit2.http.QueryMap;
11 15  
12 16 public interface ApiService {
13   - String URL_DEBUG = "https://ymzx.asia-cloud.com/api/";
14   - String URL_RELEASE = "https://ymzx.asia-cloud.com/api/";
  17 + String URL_DEBUG = "https://apiwjj.cnlive.com/Daren/moment/";
  18 + String URL_RELEASE = "https://apiwjj.cnlive.com/Daren/moment/";
  19 +
  20 +
  21 + /**
  22 + * 版本更新
  23 + *
  24 + * @return
  25 + */
  26 + @GET("friendMoment.action")
  27 + Observable<Result<BaseInfo<MomentData>>> getVersion(@QueryMap() Map<String, String> map);
15 28  
16 29 /**
17 30 * 获取应用程序信息
... ... @@ -19,6 +32,7 @@ public interface ApiService {
19 32 * @param app_id 应用程序ID
20 33 * @return AppInfo
21 34 */
  35 + @Headers("cache:60")
22 36 @GET("apps/info")
23 37 Observable<Result<AppInfo>> appInfo(@Query("app_id") int app_id);
24 38  
... ...
app/src/main/java/com/cnlive/libs/demo/data/network/testapi/BaseInfo.java 0 → 100644
  1 +package com.cnlive.libs.demo.data.network.testapi;
  2 +
  3 +import com.cnlive.libs.base.data.model.IBaseInfo;
  4 +
  5 +public class BaseInfo<T> implements IBaseInfo {
  6 + //TODO 接口错误信息 需要自定义
  7 + private String errorCode = "0";
  8 + private String errorMessage = "";
  9 +
  10 + //TODO 接口内容数据 需要自定义
  11 + private T data;
  12 +
  13 + public void setCode(String status_code) {
  14 + this.errorCode = status_code;
  15 + }
  16 +
  17 + public String getCode() {
  18 + return errorCode;
  19 + }
  20 +
  21 + public void setMessage(String errorMessage) {
  22 + this.errorMessage = errorMessage;
  23 + }
  24 +
  25 + public String getMessage() {
  26 + return errorMessage;
  27 + }
  28 +
  29 + public void setData(T data) {
  30 + this.data = data;
  31 + }
  32 +
  33 + public T getData() {
  34 + return data;
  35 + }
  36 +}
... ...
app/src/main/java/com/cnlive/libs/demo/data/network/testapi/MomentData.java 0 → 100644
  1 +package com.cnlive.libs.demo.data.network.testapi;
  2 +
  3 +import java.io.Serializable;
  4 +import java.util.List;
  5 +
  6 +public class MomentData implements Serializable {
  7 +
  8 + private boolean lastPage = true;
  9 + //新消息提醒
  10 + private MomentIcon icon;
  11 + //朋友圈列表
  12 + private List<FriendItem> moments;
  13 + //个人相册列表
  14 + private List<PhotoItem> items;
  15 +
  16 + public static class MomentIcon implements Serializable{
  17 + private String nickName;
  18 + private String userId;
  19 + private String image;
  20 +
  21 + public String getNickName() {
  22 + return nickName;
  23 + }
  24 +
  25 + public void setNickName(String nickName) {
  26 + this.nickName = nickName;
  27 + }
  28 +
  29 + public String getUserId() {
  30 + return userId;
  31 + }
  32 +
  33 + public void setUserId(String userId) {
  34 + this.userId = userId;
  35 + }
  36 +
  37 + public String getImage() {
  38 + return image;
  39 + }
  40 +
  41 + public void setImage(String image) {
  42 + this.image = image;
  43 + }
  44 + }
  45 +
  46 + public static class MomentVideo implements Serializable{
  47 + private String time;
  48 + private String imageUrl;
  49 + private String videoId;
  50 +
  51 + public String getTime() {
  52 + return time;
  53 + }
  54 +
  55 + public void setTime(String time) {
  56 + this.time = time;
  57 + }
  58 +
  59 + public String getImageUrl() {
  60 + return imageUrl;
  61 + }
  62 +
  63 + public void setImageUrl(String imageUrl) {
  64 + this.imageUrl = imageUrl;
  65 + }
  66 +
  67 + public String getVideoId() {
  68 + return videoId;
  69 + }
  70 +
  71 + public void setVideoId(String videoId) {
  72 + this.videoId = videoId;
  73 + }
  74 + }
  75 +
  76 + public static class PhotoItem implements Serializable{
  77 + private String sid;
  78 + private String text;
  79 + private long ts;
  80 + private String address;
  81 + private List<String> images;
  82 + private String itemId;
  83 + private int type;
  84 + private MomentVideo video;
  85 +
  86 + public String getSid() {
  87 + return sid;
  88 + }
  89 +
  90 + public void setSid(String sid) {
  91 + this.sid = sid;
  92 + }
  93 +
  94 + public String getText() {
  95 + return text;
  96 + }
  97 +
  98 + public void setText(String text) {
  99 + this.text = text;
  100 + }
  101 +
  102 + public long getTs() {
  103 + return ts;
  104 + }
  105 +
  106 + public void setTs(long ts) {
  107 + this.ts = ts;
  108 + }
  109 +
  110 + public String getAddress() {
  111 + return address;
  112 + }
  113 +
  114 + public void setAddress(String address) {
  115 + this.address = address;
  116 + }
  117 +
  118 + public List<String> getImages() {
  119 + return images;
  120 + }
  121 +
  122 + public void setImages(List<String> images) {
  123 + this.images = images;
  124 + }
  125 +
  126 + public String getItemId() {
  127 + return itemId;
  128 + }
  129 +
  130 + public void setItemId(String itemId) {
  131 + this.itemId = itemId;
  132 + }
  133 +
  134 + public int getType() {
  135 + return type;
  136 + }
  137 +
  138 + public void setType(int type) {
  139 + this.type = type;
  140 + }
  141 +
  142 + public MomentVideo getVideo() {
  143 + return video;
  144 + }
  145 +
  146 + public void setVideo(MomentVideo video) {
  147 + this.video = video;
  148 + }
  149 + }
  150 +
  151 + public static class FriendItem implements Serializable{
  152 + private PhotoItem item;
  153 + private MomentIcon icon;
  154 +
  155 + public PhotoItem getItem() {
  156 + return item;
  157 + }
  158 +
  159 + public void setItem(PhotoItem item) {
  160 + this.item = item;
  161 + }
  162 +
  163 + public MomentIcon getIcon() {
  164 + return icon;
  165 + }
  166 +
  167 + public void setIcon(MomentIcon icon) {
  168 + this.icon = icon;
  169 + }
  170 + }
  171 +
  172 + public MomentIcon getIcon() {
  173 + return icon;
  174 + }
  175 +
  176 + public void setIcon(MomentIcon icon) {
  177 + this.icon = icon;
  178 + }
  179 +
  180 + public List<PhotoItem> getItems() {
  181 + return items;
  182 + }
  183 +
  184 + public void setItems(List<PhotoItem> items) {
  185 + this.items = items;
  186 + }
  187 +
  188 + public List<FriendItem> getMoments() {
  189 + return moments;
  190 + }
  191 +
  192 + public void setMoments(List<FriendItem> moments) {
  193 + this.moments = moments;
  194 + }
  195 +
  196 + public boolean isLastPage() {
  197 + return lastPage;
  198 + }
  199 +
  200 + public void setLastPage(boolean lastPage) {
  201 + this.lastPage = lastPage;
  202 + }
  203 +}
0 204 \ No newline at end of file
... ...
app/src/main/java/com/cnlive/libs/demo/mvptest/ui/activity/LaunchActivity.java
... ... @@ -6,14 +6,25 @@ import android.os.Bundle;
6 6 import android.util.Log;
7 7 import android.view.View;
8 8  
  9 +import com.cnlive.libs.base.data.network.Subscriber;
9 10 import com.cnlive.libs.base.frame.activity.MvpViewStateBindingActivity;
10 11 import com.cnlive.libs.demo.R;
11 12 import com.cnlive.libs.demo.ToastTest;
  13 +import com.cnlive.libs.demo.data.network.testapi.ApiRequest;
  14 +import com.cnlive.libs.demo.data.network.testapi.ApiService;
  15 +import com.cnlive.libs.demo.data.network.testapi.BaseInfo;
  16 +import com.cnlive.libs.demo.data.network.testapi.MomentData;
12 17 import com.cnlive.libs.demo.databinding.ActivityLaunchBinding;
13 18 import com.cnlive.libs.demo.mvptest.frame.presenter.LaunchPresenter;
14 19 import com.cnlive.libs.demo.mvptest.frame.view.LaunchView;
15 20 import com.cnlive.libs.demo.mvptest.model.LaunchListItemData;
16 21 import com.cnlive.libs.demo.mvptest.model.LaunchPage;
  22 +import com.google.gson.Gson;
  23 +
  24 +import java.util.HashMap;
  25 +import java.util.Map;
  26 +
  27 +import static com.cnlive.libs.demo.RequestTest.subscriberBuild;
17 28  
18 29 public class LaunchActivity extends MvpViewStateBindingActivity<LaunchView, LaunchPresenter, LaunchPage, ActivityLaunchBinding> {
19 30  
... ... @@ -30,7 +41,7 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
30 41 @Override
31 42 protected void onPostCreate(Bundle savedInstanceState) {
32 43 super.onPostCreate(savedInstanceState);
33   - getViewData().getAdapter().setItemClickListener((id,item) -> {
  44 + getViewData().getAdapter().setItemClickListener((id, item) -> {
34 45 Log.e("DemoView", "Click " + item.getName());
35 46 });
36 47 }
... ... @@ -49,8 +60,8 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
49 60 public void onClick(View view) {
50 61 switch (view.getId()) {
51 62 case R.id.click:
52   - getViewData().setTime(2);
53   - startActivity(new Intent(this, LaunchActivity.class));
  63 +// getViewData().setTime(2);
  64 +// startActivity(new Intent(this, LaunchActivity.class));
54 65 break;
55 66 }
56 67 }
... ... @@ -63,8 +74,23 @@ public class LaunchActivity extends MvpViewStateBindingActivity&lt;LaunchView, Laun
63 74 private int index = 1;
64 75  
65 76 public void onAddItemClick(View view) {
66   - getViewData().getAdapter().addItem(new LaunchListItemData("" + index++));
67   - getViewData().setTime(getViewData().getAdapter().getItemCount());
  77 + Map<String, String> requestMap = new HashMap<>();
  78 + requestMap.put("sid", "815510");
  79 + ApiRequest.subscribe(
  80 + ApiRequest.service(ApiService.class, this).getVersion(requestMap),
  81 + new Subscriber<BaseInfo<MomentData>>(this, subscriberBuild) {
  82 + @Override
  83 + public void onCompleted(String code, String message, BaseInfo<MomentData> data) {
  84 + Log.e("DemoView", "结果 " + new Gson().toJson(data));
  85 + }
  86 +
  87 + @Override
  88 + public void onError(String code, String message) {
  89 + Log.e("DemoView", "错误 " + code + ":" + message);
  90 + }
  91 + });
  92 +// getViewData().getAdapter().addItem(new LaunchListItemData("" + index++));
  93 +// getViewData().setTime(getViewData().getAdapter().getItemCount());
68 94 }
69 95  
70 96 public void onMainItemClick(View view) {
... ...
cnlive/build.gradle
... ... @@ -78,7 +78,7 @@ ext {
78 78 siteUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo'
79 79 gitUrl = 'http://bj.gitlab.cnlive.com/cnlive-team/CNVideoDemo.git'
80 80  
81   - libraryVersion = '0.3.0'
  81 + libraryVersion = '0.3.3'
82 82  
83 83 developerId = 'CNlive'
84 84 developerName = 'Granzon'
... ...
cnlive/src/main/java/com/cnlive/libs/base/data/network/OffLineInterceptor.java 0 → 100644
  1 +package com.cnlive.libs.base.data.network;
  2 +
  3 +import android.content.Context;
  4 +
  5 +import java.io.IOException;
  6 +
  7 +import okhttp3.Interceptor;
  8 +import okhttp3.Request;
  9 +
  10 +/**
  11 + * Created by xiansong on 2018/4/4.
  12 + */
  13 +
  14 +public class OffLineInterceptor implements Interceptor {
  15 +
  16 + private static final int TIMEOUT_DISCONNECT = 60 * 60 * 24 * 7; //7天
  17 +
  18 + private Context mContext;
  19 +
  20 + protected OffLineInterceptor(Context context) {
  21 + mContext = context;
  22 + }
  23 +
  24 + @Override
  25 + public okhttp3.Response intercept(Chain chain) throws IOException {
  26 + Request request = chain.request();
  27 + //离线的时候为7天的缓存。
  28 + if (!NetworkUtil.isConnectInternet(mContext)) {
  29 + request = request.newBuilder()
  30 + .header("Cache-Control", "public, only-if-cached, max-stale=" + TIMEOUT_DISCONNECT)
  31 + .build();
  32 + }
  33 + return chain.proceed(request);
  34 + }
  35 +}
... ...
cnlive/src/main/java/com/cnlive/libs/base/data/network/OnLineInterceptor.java 0 → 100644
  1 +package com.cnlive.libs.base.data.network;
  2 +
  3 +import java.io.IOException;
  4 +
  5 +import okhttp3.Interceptor;
  6 +
  7 +/**
  8 + * Created by xiansong on 2018/4/4.
  9 + */
  10 +
  11 +public class OnLineInterceptor implements Interceptor {
  12 +
  13 + private static final int TIMEOUT_CONNECT = 5; //5秒
  14 +
  15 + @Override
  16 + public okhttp3.Response intercept(Chain chain) throws IOException {
  17 + //获取retrofit @headers里面的参数,参数可以自己定义,在本例我自己定义的是cache,跟@headers里面对应就可以了
  18 + String cache = chain.request().header("cache");
  19 + okhttp3.Response originalResponse = chain.proceed(chain.request());
  20 + String cacheControl = originalResponse.header("Cache-Control");
  21 + //如果cacheControl为空,就让他TIMEOUT_CONNECT秒的缓存,本例是5秒,方便观察。注意这里的cacheControl是服务器返回的
  22 + if (cacheControl == null) {
  23 + //如果cache没值,缓存时间为TIMEOUT_CONNECT,有的话就为cache的值
  24 + if (cache == null || "".equals(cache)) {
  25 + cache = TIMEOUT_CONNECT + "";
  26 + }
  27 + originalResponse = originalResponse.newBuilder()
  28 + .header("Cache-Control", "public, max-age=" + cache)
  29 + .build();
  30 + return originalResponse;
  31 + } else {
  32 + return originalResponse;
  33 + }
  34 + }
  35 +}
0 36 \ No newline at end of file
... ...
cnlive/src/main/java/com/cnlive/libs/base/data/network/RetrofitUtil.java
... ... @@ -24,6 +24,9 @@ import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
24 24 import retrofit2.converter.gson.GsonConverterFactory;
25 25  
26 26 public class RetrofitUtil {
  27 +
  28 + private static Context context;
  29 +
27 30 private static boolean DEBUG = AppConfig.isDebug();
28 31  
29 32 private static final String TAG = "RetrofitUtil";
... ... @@ -42,8 +45,8 @@ public class RetrofitUtil {
42 45 return getRestAPI(null, false, showLog, 5000, 5000, endpoint, service);
43 46 }
44 47  
45   - private static <T> T getRestAPI(Context context, boolean hasCache, boolean showLog, int readTimeout, int connectTimeout, String endpoint, Class<T> service) {
46   -
  48 + public static <T> T getRestAPI(Context context, boolean hasCache, boolean showLog, int readTimeout, int connectTimeout, String endpoint, Class<T> service) {
  49 + RetrofitUtil.context = context;
47 50 OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder();
48 51  
49 52 try {
... ... @@ -71,8 +74,10 @@ public class RetrofitUtil {
71 74 okHttpClientBuilder.readTimeout(readTimeout, TimeUnit.SECONDS);
72 75 }
73 76 if (hasCache) {
  77 + okHttpClientBuilder.addNetworkInterceptor(new OnLineInterceptor());
  78 + okHttpClientBuilder.addInterceptor(new OffLineInterceptor(context));
  79 +
74 80 okHttpClientBuilder.cache(new Cache(context.getCacheDir(), 10 * 1024 * 1024));
75   - okHttpClientBuilder.addNetworkInterceptor(new ClientInterceptor(context));
76 81 }
77 82  
78 83 if (showLog) {
... ... @@ -91,5 +96,4 @@ public class RetrofitUtil {
91 96 return retrofit.create(service);
92 97 }
93 98  
94   -
95 99 }
... ...
cnlive/src/main/java/com/cnlive/libs/base/frame/ReflectUtil.java
... ... @@ -32,11 +32,6 @@ public class ReflectUtil {
32 32 public static Object invoke(Object obj, String methodName, Object data) {
33 33 Object result = null;
34 34 try {
35   - Log.e("DemOView", "--------------------Test " + methodName + " : " + data.getClass());
36   - for (Method method : obj.getClass().getMethods()) {
37   - Log.e("DemOView", "Test " + method.getName() + " : " + method.getParameterAnnotations().toString());
38   - }
39   -
40 35 Method method = obj.getClass().getMethod(methodName, new Class[]{data.getClass()});
41 36 result = method.invoke(obj, data);
42 37 } catch (Exception e) {
... ... @@ -45,6 +40,17 @@ public class ReflectUtil {
45 40 return result;
46 41 }
47 42  
  43 + public static Object invoke(Object obj, String methodName) {
  44 + Object result = null;
  45 + try {
  46 + Method method = obj.getClass().getMethod(methodName);
  47 + result = method.invoke(obj);
  48 + } catch (Exception e) {
  49 + LogUtil.e(TAG, "The page data type is not match ", e);
  50 + }
  51 + return result;
  52 + }
  53 +
48 54 public static <R> R instance(int position, Class type, Object data) {
49 55 R result = null;
50 56 try {
... ...
cnlive/src/main/java/com/cnlive/libs/base/frame/adapter/DataBindingRecyclerAdapter.java
... ... @@ -62,12 +62,14 @@ public abstract class DataBindingRecyclerAdapter&lt;D, T extends ViewDataBinding&gt; e
62 62 }
63 63  
64 64 @Override
65   - public void onBindViewHolder(DataBindingViewHolder holder, int position) {
  65 + public void onBindViewHolder(final DataBindingViewHolder holder, int position) {
66 66 ReflectUtil.invoke(holder.binding, "setData", getList().get(position));
67 67 ReflectUtil.invoke(holder.binding, "setClick", new Click(position) {
68 68 @Override
69 69 public void onClick(View v) {
70   - if (listener != null) listener.onItemClick(v.getId(), getList().get(position));
  70 +// if (listener != null) listener.onItemClick(v.getId(), getList().get(position));
  71 + if (listener != null)
  72 + listener.onItemClick(v.getId(), (D) ReflectUtil.invoke(holder.binding, "getData"));
71 73 }
72 74 }, View.OnClickListener.class);
73 75 holder.binding.executePendingBindings();
... ...