Skip to content

Commit

Permalink
全局请求头设置方式修改,可以实时获取header更新内容
Browse files Browse the repository at this point in the history
  • Loading branch information
lygttpod committed Mar 3, 2019
1 parent cd4dfc2 commit 6b68f70
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 37 deletions.
20 changes: 16 additions & 4 deletions app/src/main/java/com/allen/rxhttputils/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.allen.library.RxHttpUtils;
import com.allen.library.config.OkHttpConfig;
import com.allen.library.cookie.store.SPCookieStore;
import com.allen.library.interfaces.BuildHeadersListener;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -22,8 +24,6 @@

public class App extends Application {

private Map<String, Object> headerMaps = new HashMap<>();

@Override
public void onCreate() {
super.onCreate();
Expand Down Expand Up @@ -64,8 +64,20 @@ private void initCustomRxHttpUtils() {

OkHttpClient okHttpClient = new OkHttpConfig
.Builder(this)
//全局的请求头信息
.setHeaders(headerMaps)
//添加公共请求头
.setHeaders(new BuildHeadersListener() {
@Override
public Map<String, String> buildHeaders() {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("appVersion", BuildConfig.VERSION_NAME);
hashMap.put("client", "android");
hashMap.put("token", "your_token");
hashMap.put("other_header", URLEncoder.encode("中文需要转码"));
return hashMap;
}
})
//添加自定义拦截器
//.setAddInterceptor()
//开启缓存策略(默认false)
//1、在有网络的时候,先去读缓存,缓存时间到了,再去访问网络获取数据;
//2、在没有网络的时候,去读缓存中的数据。
Expand Down
18 changes: 11 additions & 7 deletions app/src/main/java/com/allen/rxhttputils/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.allen.library.cookie.store.MemoryCookieStore;
import com.allen.library.download.DownloadObserver;
import com.allen.library.interceptor.Transformer;
import com.allen.library.interfaces.BuildHeadersListener;
import com.allen.library.interfaces.ILoadingView;
import com.allen.library.observer.CommonObserver;
import com.allen.library.observer.StringObserver;
Expand Down Expand Up @@ -93,18 +94,13 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClick(View v) {
responseTv.setText("");

Map<String, Object> headerMaps = new TreeMap<>();

headerMaps.put("header1", "header1");
headerMaps.put("header2", "header2");

switch (v.getId()) {

case R.id.global_http:
RxHttpUtils
.createApi(ApiService.class)
.getBook()
.compose(Transformer.<BookBean>switchSchedulers( loading_dialog))
.compose(Transformer.<BookBean>switchSchedulers(loading_dialog))
.subscribe(new CommonObserver<BookBean>() {

//默认false 隐藏onError的提示
Expand Down Expand Up @@ -292,7 +288,15 @@ protected void onSuccess(Top250Bean top250Bean) {
//单个请求的baseUrl
.baseUrl("https://api.douban.com/")
//单个请求的header
.addHeaders(headerMaps)
.addHeaders(new BuildHeadersListener() {
@Override
public Map<String, String> buildHeaders() {
Map<String, String> headerMaps = new TreeMap<>();
headerMaps.put("header1", "header1");
headerMaps.put("header2", "header2");
return headerMaps;
}
})
//单个请求是否开启缓存
.cache(true)
//单个请求的缓存路径及缓存大小,不设置的话有默认值
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 218
versionName "2.1.9"
versionCode 220
versionName "2.2.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand All @@ -29,5 +29,5 @@ dependencies {
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
compile 'com.squareup.retrofit2:converter-scalars:2.4.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
}
17 changes: 13 additions & 4 deletions library/src/main/java/com/allen/library/config/OkHttpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.allen.library.interceptor.NetCacheInterceptor;
import com.allen.library.interceptor.NoNetCacheInterceptor;
import com.allen.library.interceptor.RxHttpLogger;
import com.allen.library.interfaces.BuildHeadersListener;

import java.io.File;
import java.io.InputStream;
Expand Down Expand Up @@ -68,7 +69,6 @@ public static OkHttpClient getOkHttpClient() {

public static class Builder {
public Context context;
private Map<String, Object> headerMaps;
private boolean isDebug;
private boolean isCache;
private int cacheTime = 60;
Expand All @@ -83,13 +83,14 @@ public static class Builder {
private String password;
private InputStream[] certificates;
private Interceptor[] interceptors;
private BuildHeadersListener buildHeadersListener;

public Builder(Context context) {
this.context = context;
}

public Builder setHeaders(Map<String, Object> headerMaps) {
this.headerMaps = headerMaps;
public Builder setHeaders(BuildHeadersListener buildHeadersListener) {
this.buildHeadersListener = buildHeadersListener;
return this;
}

Expand Down Expand Up @@ -201,7 +202,15 @@ private void setDebugConfig() {
* 配置headers
*/
private void setHeadersConfig() {
okHttpClientBuilder.addInterceptor(new HeaderInterceptor(headerMaps));
if (buildHeadersListener != null) {
okHttpClientBuilder.addInterceptor(new HeaderInterceptor() {
@Override
public Map<String, String> buildHeaders() {
return buildHeadersListener.buildHeaders();
}
});
}

}

/**
Expand Down
18 changes: 12 additions & 6 deletions library/src/main/java/com/allen/library/http/SingleRxHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import com.allen.library.interceptor.NetCacheInterceptor;
import com.allen.library.interceptor.NoNetCacheInterceptor;
import com.allen.library.interceptor.RxHttpLogger;
import com.allen.library.interfaces.BuildHeadersListener;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -38,7 +38,7 @@ public class SingleRxHttp {

private String baseUrl;

private Map<String, Object> headerMaps = new HashMap<>();
private BuildHeadersListener buildHeadersListener;

private boolean isShowLog = true;
private boolean cache = false;
Expand Down Expand Up @@ -95,8 +95,8 @@ public SingleRxHttp addCallAdapterFactory(CallAdapter.Factory factory) {
return this;
}

public SingleRxHttp addHeaders(Map<String, Object> headerMaps) {
this.headerMaps = headerMaps;
public SingleRxHttp addHeaders(BuildHeadersListener buildHeadersListener) {
this.buildHeadersListener = buildHeadersListener;
return this;
}

Expand Down Expand Up @@ -289,8 +289,14 @@ private OkHttpClient.Builder getSingleOkHttpBuilder() {
singleOkHttpBuilder.connectTimeout(connectTimeout > 0 ? connectTimeout : 10, TimeUnit.SECONDS);
singleOkHttpBuilder.retryOnConnectionFailure(true);

singleOkHttpBuilder.addInterceptor(new HeaderInterceptor(headerMaps));

if (buildHeadersListener != null) {
singleOkHttpBuilder.addInterceptor(new HeaderInterceptor() {
@Override
public Map<String, String> buildHeaders() {
return buildHeadersListener.buildHeaders();
}
});
}

if (isShowLog) {
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new RxHttpLogger());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
Expand All @@ -13,26 +13,38 @@
* <p>
*
* @author Allen
* 请求拦截器 统一添加请求头使用
* 请求拦截器 统一添加请求头使用
*/

public class HeaderInterceptor implements Interceptor {
public abstract class HeaderInterceptor implements Interceptor {

private Map<String, Object> headerMaps = new TreeMap<>();
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Map<String, String> headers = buildHeaders();
if (headers == null || headers.isEmpty()) {
return chain.proceed(request);
} else {
Response response = chain.proceed(request.newBuilder()
.headers(buildHeaders(request, headers))
.build());
return response;
}

public HeaderInterceptor(Map<String, Object> headerMaps) {
this.headerMaps = headerMaps;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder request = chain.request().newBuilder();
if (headerMaps != null && headerMaps.size() > 0) {
for (Map.Entry<String, Object> entry : headerMaps.entrySet()) {
request.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
private Headers buildHeaders(Request request, Map<String, String> headerMap) {
Headers headers = request.headers();
if (headers != null) {
Headers.Builder builder = headers.newBuilder();
for (String key : headerMap.keySet()) {
builder.add(key, headerMap.get(key));
}
return builder.build();
} else {
return headers;
}
return chain.proceed(request.build());
}

public abstract Map<String, String> buildHeaders();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.allen.library.interfaces;

import java.util.Map;

/**
* <pre>
* @author : Allen
* e-mail : lygttpod@163.com
* date : 2019/03/03
* desc : 请求头interface
* </pre>
*/
public interface BuildHeadersListener {
Map<String, String> buildHeaders();
}

0 comments on commit 6b68f70

Please sign in to comment.