From 83d536cbe5d8b3da2c0e07dd0ef746287a3e9263 Mon Sep 17 00:00:00 2001 From: Qsong Date: Thu, 19 Dec 2019 17:26:38 +0800 Subject: [PATCH] =?UTF-8?q?-=E6=94=AF=E6=8C=81=E5=A4=9A=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=20-=E6=94=AF=E6=8C=81multipart=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/song/demo/AppContext.java | 5 +- .../main/java/com/song/demo/MainActivity.java | 12 +++-- .../java/com/song/demo/QSInterceptor2.java | 34 +++++++++++++ qshttp/build.gradle | 4 +- .../src/main/java/org/song/http/QSHttp.java | 3 +- .../org/song/http/framework/Interceptor.java | 4 +- .../org/song/http/framework/QSHttpClient.java | 51 +++++++++++-------- .../org/song/http/framework/QSHttpConfig.java | 16 +++--- .../song/http/framework/RequestParams.java | 9 ++-- .../java/org/song/http/framework/Utils.java | 7 ++- 10 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/com/song/demo/QSInterceptor2.java diff --git a/app/src/main/java/com/song/demo/AppContext.java b/app/src/main/java/com/song/demo/AppContext.java index f8f8a12..81918c2 100644 --- a/app/src/main/java/com/song/demo/AppContext.java +++ b/app/src/main/java/com/song/demo/AppContext.java @@ -29,7 +29,10 @@ public void onCreate() { private void init() { - QSHttp.init(QSHttpConfig.Build(this).interceptor(new QSInterceptor()).build()); + QSHttp.init(QSHttpConfig.Build(this) + .interceptor(new QSInterceptor()) + .interceptor(new QSInterceptor2()) + .build()); } diff --git a/app/src/main/java/com/song/demo/MainActivity.java b/app/src/main/java/com/song/demo/MainActivity.java index 303ead2..6aae620 100644 --- a/app/src/main/java/com/song/demo/MainActivity.java +++ b/app/src/main/java/com/song/demo/MainActivity.java @@ -48,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) { downGET(url); upLoad(url); -// parserJson(); + parserJson(); } @@ -150,7 +150,10 @@ public void upLoad(String url) { .param("bytes", new byte[1024])//multipart方式上传一个字节数组 .param("file", new File(getExternalCacheDir(), "http.txt"))//multipart方式上传一个文件 - .multipartBody("img", "image/*", "x.jpg", new byte[1024]) + + //IdentityHashMap支持重复key,需new + .multipartBody(new String("img"), "image/*", "qs.jpg", new byte[1024]) + .multipartBody(new String("img"), "image/*", "qs.jpg", new byte[1024]) .buildAndExecute(new ProgressCallback() { @Override @@ -192,7 +195,7 @@ public void onComplete(User dataUser) { }); QSHttp.postJSON("https://api.reol.top/test/json") - .header("string", "{\"status\":0,\"data\":3.6}") + .jsonBody("3.666489") .buildAndExecute(new MyHttpCallback() { @Override public void onComplete(Double dataUser) { @@ -221,7 +224,8 @@ public void onComplete(User dataUser) { }); QSHttp.postJSON("https://api.reol.top/test/json") - .header("string", "3.6") + .header("row", "row") + .jsonBody("3.6") .buildAndExecute(new QSHttpCallback() { @Override public void onComplete(String dataUser) { diff --git a/app/src/main/java/com/song/demo/QSInterceptor2.java b/app/src/main/java/com/song/demo/QSInterceptor2.java new file mode 100644 index 0000000..3b08530 --- /dev/null +++ b/app/src/main/java/com/song/demo/QSInterceptor2.java @@ -0,0 +1,34 @@ +package com.song.demo; + +import org.song.http.framework.HttpException; +import org.song.http.framework.Interceptor; +import org.song.http.framework.RequestParams; +import org.song.http.framework.ResponseParams; + +/** + * Created by song + * Contact github.com/tohodog + * Date 2019/12/18 + * 多拦截器 + */ +public class QSInterceptor2 implements Interceptor { + @Override + public ResponseParams intercept(Chain chain) throws HttpException { + + String url = chain.request().url(); + if (url != null && !url.startsWith("http")) { + url = API.HOST + url; + } + + RequestParams newRequestParams = chain.request() + .newBuild(url) + .header("Interceptor2", "Interceptor2") + //继续添加修改其他 + .build(); + + + ResponseParams responseParams = chain.proceed(newRequestParams); + //请求结果参数如有需要也可以进行修改 + return responseParams; + } +} diff --git a/qshttp/build.gradle b/qshttp/build.gradle index 65a0728..47406ad 100644 --- a/qshttp/build.gradle +++ b/qshttp/build.gradle @@ -6,8 +6,8 @@ android { minSdkVersion 9 targetSdkVersion compileSdkVersion 28 - versionCode 11 - versionName "1.4.3" + versionCode 12 + versionName "1.5.0" } buildTypes { release { diff --git a/qshttp/src/main/java/org/song/http/QSHttp.java b/qshttp/src/main/java/org/song/http/QSHttp.java index 60862a9..741f26e 100644 --- a/qshttp/src/main/java/org/song/http/QSHttp.java +++ b/qshttp/src/main/java/org/song/http/QSHttp.java @@ -99,10 +99,9 @@ public static RequestParams.Builder upload(String url) { return postMulti(url); } - //这里可以添加公共参数鉴权 + private static RequestParams.Builder build(String url, HttpEnum.RequestMethod requestMethod) { return RequestParams.Build(url) .requestMethod(requestMethod); - //.header("sessionKey", "sessionKey"); } } diff --git a/qshttp/src/main/java/org/song/http/framework/Interceptor.java b/qshttp/src/main/java/org/song/http/framework/Interceptor.java index 54bf358..ea29f10 100644 --- a/qshttp/src/main/java/org/song/http/framework/Interceptor.java +++ b/qshttp/src/main/java/org/song/http/framework/Interceptor.java @@ -9,8 +9,8 @@ public interface Interceptor { ResponseParams intercept(Chain chain) throws HttpException; interface Chain { - RequestParams request(); + RequestParams request();//请求参数 - ResponseParams proceed(RequestParams request) throws HttpException; + ResponseParams proceed(RequestParams request) throws HttpException;//流程继续 } } diff --git a/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java b/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java index 4622ec2..638810d 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java @@ -1,7 +1,7 @@ package org.song.http.framework; -import java.util.Random; +import java.util.List; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ExecutorService; @@ -21,12 +21,13 @@ public class QSHttpClient { private ExecutorService executorService; - private Interceptor interceptor; + private List interceptorList; + public QSHttpClient(IHttpTask iHttpTask, QSHttpConfig qsHttpConfig) { this.iHttpTask = iHttpTask; this.qsHttpConfig = qsHttpConfig; - interceptor = qsHttpConfig.interceptor(); + interceptorList = qsHttpConfig.interceptorList(); executorService = Executors.newFixedThreadPool(qsHttpConfig.poolSize()); } @@ -50,24 +51,10 @@ public void run() { ResponseParams response; final HttpProgress hp = isProgress ? new HttpProgress(mThreadWhat) : null; try { - if (interceptor != null) {//拦截器 - response = interceptor.intercept(new Interceptor.Chain() { - @Override - public RequestParams request() { - return _request; - } - - @Override - public ResponseParams proceed(RequestParams request) throws HttpException { - _request = request; - return access(request, hp); - } - }); - if (response == null) { - throw HttpException.Custom(interceptor.getClass().getName() + ".intercept() - return value can't null!"); - } - } else - response = access(_request, hp); + //拦截器 + response = runInterceptor(0, _request, hp); + //经过拦截器,request被改变了 + _request = response.requestParams(); response.setSuccess(true); } catch (Exception e) { e.printStackTrace(); @@ -97,6 +84,28 @@ public ResponseParams proceed(RequestParams request) throws HttpException { return mThreadWhat; } + //递归调用拦截器 + private ResponseParams runInterceptor(final int index, final RequestParams request, final HttpProgress hp) throws HttpException { + if (interceptorList == null || index >= interceptorList.size()) {//递归终点 + ResponseParams response = access(request, hp); + response.setRequestParams(request); + return response; + } + + Interceptor interceptor = interceptorList.get(index); + return interceptor.intercept(new Interceptor.Chain() { + @Override + public RequestParams request() { + return request; + } + + @Override + public ResponseParams proceed(RequestParams request) throws HttpException { + return runInterceptor(index + 1, request, hp);//递归调用 + } + }); + } + //具体联网逻辑 保证返回ResponseParams对象不为null private ResponseParams access(final RequestParams request, final HttpProgress hp) throws HttpException { ResponseParams response = null; diff --git a/qshttp/src/main/java/org/song/http/framework/QSHttpConfig.java b/qshttp/src/main/java/org/song/http/framework/QSHttpConfig.java index 7550cb6..485d8a1 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpConfig.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpConfig.java @@ -2,6 +2,9 @@ import android.app.Application; +import java.util.ArrayList; +import java.util.List; + import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSocketFactory; @@ -16,7 +19,7 @@ public class QSHttpConfig { private Application application; private HttpEnum.XX_Http xxHttp; - private Interceptor interceptor; + private List interceptorList; private SSLSocketFactory sslSocketFactory; private String[] sslHost; @@ -46,8 +49,8 @@ public HttpEnum.XX_Http xxHttp() { return xxHttp; } - public Interceptor interceptor() { - return interceptor; + public List interceptorList() { + return interceptorList; } public String[] sslHost() { @@ -102,7 +105,7 @@ public static final class Builder { private HttpEnum.XX_Http xxHttp = HttpEnum.XX_Http.OK_HTTP;// private String cachePath; - private Interceptor interceptor; + private List interceptorList; private SSLSocketFactory sslSocketFactory; private String[] sslHost; @@ -122,7 +125,7 @@ private Builder(Application application) { public QSHttpConfig build() { QSHttpConfig qsHttpConfig = new QSHttpConfig(); qsHttpConfig.debug = debug; - qsHttpConfig.interceptor = interceptor; + qsHttpConfig.interceptorList = interceptorList; qsHttpConfig.application = application; qsHttpConfig.xxHttp = xxHttp; qsHttpConfig.poolSize = poolSize; @@ -154,7 +157,8 @@ public Builder xxHttp(HttpEnum.XX_Http xxHttp) { } public Builder interceptor(Interceptor interceptor) { - this.interceptor = interceptor; + if (this.interceptorList == null) this.interceptorList = new ArrayList<>(); + this.interceptorList.add(interceptor); return this; } diff --git a/qshttp/src/main/java/org/song/http/framework/RequestParams.java b/qshttp/src/main/java/org/song/http/framework/RequestParams.java index 34b5fa8..ff9e369 100644 --- a/qshttp/src/main/java/org/song/http/framework/RequestParams.java +++ b/qshttp/src/main/java/org/song/http/framework/RequestParams.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -445,9 +446,10 @@ public RequestParams.Builder jsonBody(Object object) { String json = "{}"; if (object instanceof org.json.JSONObject) { json = object.toString(); - } else { + } else if (!(object instanceof CharSequence)) { json = JSON.toJSONString(object); - } + } else + json = String.valueOf(object); requestBody(HttpEnum.CONTENT_TYPE_JSON_ + charset, json); return this; } @@ -478,7 +480,7 @@ public RequestParams.Builder toMultiBody(String multipartType) { toMultiBodyFlag = true; this.multipartType = multipartType; if (multipartBody == null) - multipartBody = new HashMap<>(); + multipartBody = new IdentityHashMap<>();//支持重复key,内存地址要不一样 bodyType = 2; return this; } @@ -488,6 +490,7 @@ public RequestParams.Builder toMultiBody(String multipartType) { */ public RequestParams.Builder multipartBody(String key, String contentType, String filename, Object value) { toMultiBody(); + //new String(key)才能保证不同地址,key不重复 multipartBody.put(key, new RequestBody(contentType, filename, value)); return this; } diff --git a/qshttp/src/main/java/org/song/http/framework/Utils.java b/qshttp/src/main/java/org/song/http/framework/Utils.java index 40122bc..3864900 100644 --- a/qshttp/src/main/java/org/song/http/framework/Utils.java +++ b/qshttp/src/main/java/org/song/http/framework/Utils.java @@ -248,8 +248,11 @@ public static boolean checkNet() { } public static void showToast(String str) { - if (QSHttpManage.application != null && str != null) - Toast.makeText(QSHttpManage.application, str, Toast.LENGTH_LONG).show(); + if (QSHttpManage.application != null && str != null) { + Toast toast = Toast.makeText(QSHttpManage.application, str, Toast.LENGTH_LONG); + toast.setText(str); + toast.show(); + } }