Skip to content

Commit

Permalink
将网络模块中不通用功能拆分成网络拓展库
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyaoyou1212 committed May 17, 2017
1 parent 2f6da5c commit 09e26ca
Show file tree
Hide file tree
Showing 43 changed files with 239 additions and 3,004 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
testCompile 'junit:junit:4.12'
compile project(path: ':xsnow')
compile project(path: ':netexpand')
}
1 change: 1 addition & 0 deletions netexpand/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
23 changes: 23 additions & 0 deletions netexpand/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 12
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile project(path: ':xsnow')
}
17 changes: 17 additions & 0 deletions netexpand/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/xyy/android-sdk-macosx/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
12 changes: 12 additions & 0 deletions netexpand/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vise.netexpand">

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.vise.netexpand.common;

import com.vise.netexpand.mode.ApiResult;
import com.vise.netexpand.mode.ResponseCode;

/**
* @Description:
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
* @date: 17/5/17 23:59.
*/
public class ResponseHelper {

public static boolean isSuccess(ApiResult apiResult) {
if (apiResult == null) {
return false;
}
if (apiResult.getCode() == ResponseCode.HTTP_SUCCESS || ignoreSomeIssue(apiResult.getCode())) {
return true;
} else {
return false;
}
}

private static boolean ignoreSomeIssue(int code) {
switch (code) {
case ResponseCode.TIMESTAMP_ERROR://时间戳过期
case ResponseCode.ACCESS_TOKEN_EXPIRED://AccessToken错误或已过期
case ResponseCode.REFRESH_TOKEN_EXPIRED://RefreshToken错误或已过期
case ResponseCode.OTHER_PHONE_LOGIN://帐号在其它手机已登录
case ResponseCode.NO_ACCESS_TOKEN://缺少授权信息,没有AccessToken
case ResponseCode.SIGN_ERROR://签名错误
return true;
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vise.xsnow.net.convert;
package com.vise.netexpand.convert;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vise.xsnow.net.convert;
package com.vise.netexpand.convert;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.vise.xsnow.net.convert;
package com.vise.netexpand.convert;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.vise.xsnow.net.exception.ApiException;
import com.vise.xsnow.net.mode.ApiResult;
import com.vise.netexpand.common.ResponseHelper;
import com.vise.netexpand.mode.ApiResult;
import com.vise.netexpand.mode.ResponseCode;

import java.io.IOException;
import java.net.UnknownServiceException;
Expand Down Expand Up @@ -35,7 +36,7 @@ public T convert(ResponseBody value) throws IOException {
if (data == null) throw new UnknownServiceException("server back data is null");
if (data instanceof ApiResult) {
ApiResult apiResult = (ApiResult) data;
if (!ApiException.isSuccess(apiResult)) {
if (!ResponseHelper.isSuccess(apiResult)) {
throw new UnknownServiceException(apiResult.getMsg() == null ? "unknow error" : apiResult.getMsg());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vise.xsnow.net.convert;
package com.vise.netexpand.convert;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.vise.xsnow.net.func;
package com.vise.netexpand.func;

import com.vise.xsnow.net.exception.ApiException;
import com.vise.xsnow.net.mode.ApiResult;
import com.vise.netexpand.common.ResponseHelper;
import com.vise.netexpand.mode.ApiResult;

import rx.functions.Func1;

Expand All @@ -16,10 +16,9 @@ public ApiDataFunc() {

@Override
public T call(ApiResult<T> response) {
if (ApiException.isSuccess(response)) {
if (ResponseHelper.isSuccess(response)) {
return response.getData();
} else {
return (T) new ApiException(new Throwable(response.getMsg()), response.getCode());
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.vise.xsnow.net.func;
package com.vise.netexpand.func;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.vise.xsnow.net.mode.ApiResult;
import com.vise.netexpand.mode.ApiResult;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.vise.xsnow.net.interceptor;
package com.vise.netexpand.interceptor;

import android.text.TextUtils;

import com.vise.log.ViseLog;
import com.vise.netexpand.mode.ApiResult;
import com.vise.netexpand.mode.ResponseCode;
import com.vise.xsnow.common.GSONUtil;
import com.vise.xsnow.net.mode.ApiCode;
import com.vise.xsnow.net.mode.ApiResult;

import java.io.IOException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -54,17 +54,17 @@ private Response process(Chain chain) throws IOException {
ApiResult apiResult = GSONUtil.gson().fromJson(bodyString, ApiResult.class);
if (apiResult != null) {
switch (apiResult.getCode()) {
case ApiCode.Response.ACCESS_TOKEN_EXPIRED: //AccessToken错误或已过期
case ResponseCode.ACCESS_TOKEN_EXPIRED: //AccessToken错误或已过期
return processAccessTokenExpired(chain, request);
case ApiCode.Response.REFRESH_TOKEN_EXPIRED://RefreshToken错误或已过期
case ResponseCode.REFRESH_TOKEN_EXPIRED://RefreshToken错误或已过期
return processRefreshTokenExpired(chain, request);
case ApiCode.Response.OTHER_PHONE_LOGIN://帐号在其它手机已登录
case ResponseCode.OTHER_PHONE_LOGIN://帐号在其它手机已登录
return processOtherPhoneLogin(chain, request);
case ApiCode.Response.SIGN_ERROR://签名错误
case ResponseCode.SIGN_ERROR://签名错误
return processSignError(chain, request);
case ApiCode.Response.TIMESTAMP_ERROR://timestamp过期
case ResponseCode.TIMESTAMP_ERROR://timestamp过期
return processTimestampError(chain, request);
case ApiCode.Response.NO_ACCESS_TOKEN://缺少授权信息
case ResponseCode.NO_ACCESS_TOKEN://缺少授权信息
return processNoAccessToken(chain, request);
default:
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vise.xsnow.net.mode;
package com.vise.netexpand.mode;

/**
* @Description: 封装的通用服务器返回对象,可自行定义
Expand Down
23 changes: 23 additions & 0 deletions netexpand/src/main/java/com/vise/netexpand/mode/ResponseCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.vise.netexpand.mode;

/**
* @Description: Response响应码(根据服务器提供文档进行定义)
* @author: <a href="http://xiaoyaoyou1212.360doc.com">DAWI</a>
* @date: 2016-12-30 18:11
*/
public class ResponseCode {
//HTTP请求成功状态码
public static final int HTTP_SUCCESS = 0;
//AccessToken错误或已过期
public static final int ACCESS_TOKEN_EXPIRED = 10001;
//RefreshToken错误或已过期
public static final int REFRESH_TOKEN_EXPIRED = 10002;
//帐号在其它手机已登录
public static final int OTHER_PHONE_LOGIN = 10003;
//时间戳过期
public static final int TIMESTAMP_ERROR = 10004;
//缺少授权信息,没有AccessToken
public static final int NO_ACCESS_TOKEN = 10005;
//签名错误
public static final int SIGN_ERROR = 10006;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.vise.netexpand.request;

import android.content.Context;

import com.vise.netexpand.func.ApiDataFunc;
import com.vise.netexpand.func.ApiResultFunc;
import com.vise.netexpand.mode.ApiResult;
import com.vise.utils.assist.ClassUtil;
import com.vise.xsnow.net.ViseNet;
import com.vise.xsnow.net.callback.ACallback;
import com.vise.xsnow.net.func.ApiRetryFunc;
import com.vise.xsnow.net.mode.CacheResult;
import com.vise.xsnow.net.request.BaseRequest;
import com.vise.xsnow.net.request.GetRequest;
import com.vise.xsnow.net.subscriber.ApiCallbackSubscriber;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
* @Description:
* @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
* @date: 17/5/13 14:31.
*/
public class ApiGetRequest extends GetRequest {
@Override
protected <T> Observable<T> execute(Class<T> clazz) {
return apiService.get(suffixUrl, params).map(new ApiResultFunc<T>(clazz)).compose(this.<T>apiTransformer());
}

@Override
protected <T> Observable<CacheResult<T>> cacheExecute(Class<T> clazz) {
return execute(clazz).compose(ViseNet.getInstance().getApiCache().transformer(cacheMode, clazz));
}

@Override
protected <T> Subscription execute(Context context, ACallback<T> callback) {
if (isLocalCache) {
return this.cacheExecute(ClassUtil.getTClass(callback))
.subscribe(new ApiCallbackSubscriber(context, callback));
}
return this.execute(ClassUtil.getTClass(callback))
.subscribe(new ApiCallbackSubscriber(context, callback));
}

protected <T> Observable.Transformer<ApiResult<T>, T> apiTransformer() {
return new Observable.Transformer<ApiResult<T>, T>() {
@Override
public Observable<T> call(Observable<ApiResult<T>> apiResultObservable) {
return apiResultObservable
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new ApiDataFunc<T>())
.retryWhen(new ApiRetryFunc(retryCount, retryDelayMillis));
}
};
}
}
3 changes: 3 additions & 0 deletions netexpand/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">netexpand</string>
</resources>
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':app', ':xsnow'
include ':app', ':xsnow', ':netexpand'
11 changes: 7 additions & 4 deletions xsnow/src/main/java/com/vise/xsnow/common/ViseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public class ViseConfig {
public static final int MAX_AGE_ONLINE = 60;//默认最大在线缓存时间(秒)
public static final int MAX_AGE_OFFLINE = 24 * 60 * 60;//默认最大离线缓存时间(秒)

public static String API_HOST = "https://api.github.com/";//默认API主机地址
public static final String API_HOST = "https://api.github.com/";//默认API主机地址

public static final String COOKIE_PREFS = "Cookies_Prefs";//默认Cookie缓存目录

public static final int DEFAULT_TIMEOUT = 60;//默认超时时间
public static final int DEFAULT_TIMEOUT = 60;//默认超时时间(秒)
public static final int DEFAULT_MAX_IDLE_CONNECTIONS = 5;//默认空闲连接数
public static final long DEFAULT_KEEP_ALIVE_DURATION = 8;//默认心跳间隔时长
public static final long CACHE_MAX_SIZE = 10 * 1024 * 1024;//默认最大缓存大小
public static final long DEFAULT_KEEP_ALIVE_DURATION = 8;//默认心跳间隔时长(秒)
public static final long CACHE_MAX_SIZE = 10 * 1024 * 1024;//默认最大缓存大小(字节)

public static final int DEFAULT_RETRY_COUNT = 3;//默认重试次数
public static final int DEFAULT_RETRY_DELAY_MILLIS = 3000;//默认重试间隔时间(毫秒)
}
Loading

0 comments on commit 09e26ca

Please sign in to comment.