Skip to content

Commit

Permalink
新增支持添加动态的全局参数
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Aug 14, 2020
1 parent 44c6ca9 commit 18972a1
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 40 deletions.
Binary file modified EasyHttp.apk
Binary file not shown.
48 changes: 30 additions & 18 deletions HelpDoc.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#### 如何添加全局参数?

```
```java
// 添加全局请求参数
EasyConfig.getInstance().addHeader("token", "abc");
// 添加全局请求头
EasyConfig.getInstance().addParam("token", "abc");
```

#### 如何在请求中忽略某个全局参数?
#### 如何定义全局的动态参数?

```java
EasyConfig.getInstance().setInterceptor(new IRequestInterceptor() {

@Override
public void intercept(String url, String tag, HttpParams params, HttpHeaders headers) {
headers.put("timestamp", String.valueOf(System.currentTimeMillis()));
}
});
```

#### 如何在请求中忽略某个全局参数?

```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -24,7 +36,7 @@ public final class XxxApi implements IRequestApi {

#### 如何获取服务器配置?

```
```java
IRequestServer server = EasyConfig.getInstance().getServer();
// 获取当前全局的服务器主机地址
String host = server.getHost();
Expand All @@ -36,7 +48,7 @@ String path = server.getPath();

* 先定义一个服务器配置

```
```java
public class XxxServer implements IRequestServer {

@Override
Expand All @@ -53,13 +65,13 @@ public class XxxServer implements IRequestServer {

* 再将它应用到全局配置中

```
```java
EasyConfig.getInstance().setServer(new XxxServer());
```

* 如果只是针对某个接口可以这样配置

```
```java
public final class XxxApi extends XxxServer implements IRequestApi {

@Override
Expand All @@ -71,7 +83,7 @@ public final class XxxApi extends XxxServer implements IRequestApi {

* 如果不想单独定义一个类,也可以这样写

```
```java
public final class XxxApi implements IRequestServer, IRequestApi {

@Override
Expand All @@ -95,7 +107,7 @@ public final class XxxApi implements IRequestServer, IRequestApi {

* 以表单的形式提交参数(默认)

```
```java
public class XxxServer implements IRequestServer {

@Override
Expand All @@ -117,7 +129,7 @@ public class XxxServer implements IRequestServer {

* 以 Json 的形式提交参数

```
```java
public class XxxServer implements IRequestServer {

@Override
Expand Down Expand Up @@ -148,7 +160,7 @@ public class XxxServer implements IRequestServer {

#### 如何忽略某个参数?

```
```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -163,7 +175,7 @@ public final class XxxApi implements IRequestApi {

#### 如何传入请求头?

```
```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -178,7 +190,7 @@ public final class XxxApi implements IRequestApi {

#### 如何重命名参数名称?

```
```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -195,7 +207,7 @@ public final class XxxApi implements IRequestApi {

* 使用 File 对象上传

```
```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -209,7 +221,7 @@ public final class XxxApi implements IRequestApi {

* 使用 InputStream 对象上传

```
```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -223,7 +235,7 @@ public final class XxxApi implements IRequestApi {

* 使用 RequestBody 对象上传

```
```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -237,7 +249,7 @@ public final class XxxApi implements IRequestApi {

#### 如何上传文件列表?

```
```java
public final class XxxApi implements IRequestApi {

@Override
Expand All @@ -251,12 +263,12 @@ public final class XxxApi implements IRequestApi {

#### 如何设置超时重试?

```
```java
EasyConfig.getInstance().setRetryCount(3);
```

#### 如何设置不打印日志?

```
```java
EasyConfig.getInstance().setLogEnabled(false);
```
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

dependencies {
implementation 'com.hjq:http:8.0'
implementation 'com.hjq:http:8.2'
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
implementation 'com.google.code.gson:gson:2.8.5'
}
Expand Down Expand Up @@ -127,8 +127,8 @@
EasyHttp.post(this)
.api(new LoginApi()
.setUserName("Android 轮子哥")
.setPassword("123456"))
.setUserName("Android 轮子哥")
.setPassword("123456"))
.request(new HttpCallback<HttpData<LoginBean>>(activity) {

@Override
Expand Down Expand Up @@ -214,14 +214,15 @@

| 功能 | Retrofit 框架 | EasyHttp 框架 |
| :----: | :------: | :-----: |
| 动态 Host | 不支持 | 支持 |
| 动态 Host | 不支持 | 支持 |
| 全局参数 | 不支持 | 支持 |
| 动态参数 | 不支持 | 支持 |
| 超时重试 | 不支持 | 支持 |
| 极速下载 | 不支持 | 支持 |
| 下载校验 | 不支持 | 支持 |
| 注解数量 | 25 个 | 3 个 |
| 上传文件 | RequestBody | File / InputStream |
| 请求管理 | 需要封装 | 自动管控 |
| 生命周期 | 需要封装 | 自动管控 |

#### 作者的其他开源项目

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "com.hjq.http.demo"
minSdkVersion 14
targetSdkVersion 28
versionCode 80
versionName "8.0"
versionCode 82
versionName "8.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/hjq/http/demo/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import android.app.Application;

import com.hjq.http.EasyConfig;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.config.IRequestServer;
import com.hjq.http.demo.http.model.RequestHandler;
import com.hjq.http.demo.http.server.ReleaseServer;
import com.hjq.http.demo.http.server.TestServer;
import com.hjq.http.model.HttpHeaders;
import com.hjq.http.model.HttpParams;
import com.hjq.toast.ToastUtils;

import okhttp3.OkHttpClient;
Expand All @@ -33,6 +36,13 @@ public void onCreate() {
.setServer(server)
// 设置请求处理策略
.setHandler(new RequestHandler(this))
// 设置请求参数拦截器
.setInterceptor(new IRequestInterceptor() {
@Override
public void intercept(String url, String tag, HttpParams params, HttpHeaders headers) {
headers.put("timestamp", String.valueOf(System.currentTimeMillis()));
}
})
// 设置请求重试次数
.setRetryCount(1)
// 添加全局请求参数
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 26
versionCode 80
versionName "8.0"
versionCode 82
versionName "8.2"
}
}

Expand All @@ -27,7 +27,7 @@ publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'http'
version = '8.0'
version = '8.2'
description = 'Easy-to-use network request framework'
website = "https://github.com/getActivity/EasyHttp"
}
Expand Down
18 changes: 15 additions & 3 deletions library/src/main/java/com/hjq/http/EasyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hjq.http.config.ILogStrategy;
import com.hjq.http.config.IRequestHandler;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.config.IRequestServer;
import com.hjq.http.config.LogStrategy;
import com.hjq.http.config.RequestServer;
Expand Down Expand Up @@ -40,8 +41,10 @@ public static EasyConfig with(OkHttpClient client) {

/** 服务器配置 */
private IRequestServer mServer;
/** 请求拦截器 */
/** 请求处理器 */
private IRequestHandler mHandler;
/** 请求拦截器 */
private IRequestInterceptor mInterceptor;
/** 日志打印策略 */
private ILogStrategy mLogStrategy;

Expand Down Expand Up @@ -83,6 +86,11 @@ public EasyConfig setHandler(IRequestHandler handler) {
return this;
}

public EasyConfig setInterceptor(IRequestInterceptor interceptor) {
mInterceptor = interceptor;
return this;
}

public EasyConfig setClient(OkHttpClient client) {
mClient = client;
return this;
Expand Down Expand Up @@ -125,7 +133,7 @@ public EasyConfig setLogTag(String tag) {

public EasyConfig setRetryCount(int count) {
if (count < 0) {
// 重试次数必须大于0
// 重试次数必须大于等于 0 次
throw new IllegalArgumentException("The number of retries must be greater than 0");
}
mRetryCount = count;
Expand All @@ -134,7 +142,7 @@ public EasyConfig setRetryCount(int count) {

public void setRetryTime(long time) {
if (time < 0) {
// 重试时间必须大于0
// 重试时间必须大于等于 0 毫秒
throw new IllegalArgumentException("The retry time must be greater than 0");
}
mRetryTime = time;
Expand All @@ -148,6 +156,10 @@ public IRequestHandler getHandler() {
return mHandler;
}

public IRequestInterceptor getInterceptor() {
return mInterceptor;
}

public OkHttpClient getClient() {
return mClient;
}
Expand Down
14 changes: 5 additions & 9 deletions library/src/main/java/com/hjq/http/callback/BaseCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@
*/
public abstract class BaseCallback implements Callback {

/**
* 请求任务对象
*/
/** 请求任务对象 */
private CallProxy mCall;
/**
* 当前重试次数
*/

/** 当前重试次数 */
private int mRetryCount;
/**
* 生命周期管理
*/

/** 生命周期管理 */
private LifecycleOwner mLifecycleOwner;

BaseCallback(LifecycleOwner lifecycleOwner, CallProxy call) {
Expand Down
23 changes: 23 additions & 0 deletions library/src/main/java/com/hjq/http/config/IRequestInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.hjq.http.config;

import com.hjq.http.model.HttpHeaders;
import com.hjq.http.model.HttpParams;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/08/05
* desc : 请求参数拦截器
*/
public interface IRequestInterceptor {

/**
* 开始请求之前调用
*
* @param url 请求地址
* @param tag 请求标记
* @param params 请求参数
* @param headers 请求头参数
*/
void intercept(String url, String tag, HttpParams params, HttpHeaders headers);
}
5 changes: 5 additions & 0 deletions library/src/main/java/com/hjq/http/request/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.hjq.http.annotation.HttpRename;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.config.IRequestHost;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.config.IRequestPath;
import com.hjq.http.config.IRequestServer;
import com.hjq.http.config.IRequestType;
Expand Down Expand Up @@ -254,6 +255,10 @@ public Call create() {
}

String url = mRequestHost.getHost() + mRequestPath.getPath() + mRequestApi.getApi();
IRequestInterceptor interceptor = EasyConfig.getInstance().getInterceptor();
if (interceptor != null) {
interceptor.intercept(url, mTag, params, headers);
}
return mClient.newCall(create(url, mTag, params, headers, type));
}

Expand Down

0 comments on commit 18972a1

Please sign in to comment.