From ee3894c447a76956341549e09e1ae47af8e1606c Mon Sep 17 00:00:00 2001 From: QSong <764469385@qq.com> Date: Thu, 26 Nov 2020 20:54:34 +0800 Subject: [PATCH 1/6] sync --- .../http/framework/HttpResultHandler.java | 10 ++++---- .../org/song/http/framework/QSHttpClient.java | 3 ++- .../song/http/framework/ThreadHandler.java | 23 +++++++++++++------ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java b/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java index f5fb8e3..3935eb5 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java +++ b/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java @@ -11,19 +11,19 @@ public class HttpResultHandler { /** * 联网完成后进行数据处理 */ - public static void onComplete(ResponseParams response) { + public static void onComplete(ResponseParams response, boolean isSync) { if (response.isSuccess()) - ThreadHandler.Success(response); + ThreadHandler.Success(response, isSync); else - ThreadHandler.Failure(response); + ThreadHandler.Failure(response, isSync); } public static void dealCache(ResponseParams response) { HttpCache httpCache = HttpCache.instance(); try { - if (response.isSuccess()) + if (response.isSuccess()) { httpCache.checkAndSaveCache(response); - else { + } else { boolean b = httpCache.checkAndGetErrCache(response); if (b) { response.setSuccess(true); 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 a985bc4..59a51af 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java @@ -42,6 +42,7 @@ public QSHttpClient(IHttpTask iHttpTask, QSHttpConfig qsHttpConfig) { public int execute(final RequestParams request, HttpCallback cb) { final int mThreadWhat = ThreadHandler.AddHttpCallback(cb); final boolean isProgress = cb instanceof IHttpProgress; + final boolean isSync = false; executorService.submit(new Runnable() { @@ -80,7 +81,7 @@ public void run() { HttpResultHandler.dealParser(response); } finally { - HttpResultHandler.onComplete(response); + HttpResultHandler.onComplete(response, isSync); } } }); diff --git a/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java b/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java index 8816713..e0b717e 100644 --- a/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java +++ b/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java @@ -5,18 +5,21 @@ import android.os.Message; import android.util.SparseArray; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /* * Created by song on 2016/9/18. * 子线程联网任务 回调主线程 类 */ public class ThreadHandler extends Handler { - private int mThreadWhat = 233; - private SparseArray sparseArray; + private int mThreadWhat = 19930411; + private Map sparseArray = new ConcurrentHashMap<>(); +// private SparseArray sparseArray = new SparseArray<>(); private ThreadHandler() { super(Looper.getMainLooper()); - sparseArray = new SparseArray<>(); } private static ThreadHandler instance; @@ -103,18 +106,21 @@ public static void removeHttpCallback(int mThreadWhat) { /** * 成功的处理 */ - public static void Success(ResponseParams obj) { + public static void Success(ResponseParams obj, boolean sync) { Message msg = Message.obtain(); msg.what = obj.requestID(); msg.arg1 = HttpEnum.HTTP_SUCCESS; msg.obj = obj; - getInstance().sendMessage(msg); + if (sync) + getInstance().handleMessage(msg); + else + getInstance().sendMessage(msg); } /** * 失败的处理 */ - public static void Failure(ResponseParams obj) { + public static void Failure(ResponseParams obj, boolean sync) { Message msg = Message.obtain(); msg.what = obj.requestID(); msg.arg1 = HttpEnum.HTTP_FAILURE; @@ -122,7 +128,10 @@ public static void Failure(ResponseParams obj) { if (!(e instanceof HttpException)) e = HttpException.Run(e); msg.obj = ((HttpException) e).responseParams(obj); - getInstance().sendMessage(msg); + if (sync) + getInstance().handleMessage(msg); + else + getInstance().sendMessage(msg); } /** From 2e4bb31b986617a542751f0499b7fc468027e6f7 Mon Sep 17 00:00:00 2001 From: QSong <764469385@qq.com> Date: Thu, 26 Nov 2020 21:52:46 +0800 Subject: [PATCH 2/6] =?UTF-8?q?post=5Fbody=E5=A2=9E=E5=8A=A0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/song/http/framework/java/HttpURLConnectionTask.java | 3 ++- .../src/main/java/org/song/http/framework/java/ReadHelp.java | 2 +- .../src/main/java/org/song/http/framework/ok/OkHttpTask.java | 2 +- .../java/org/song/http/framework/ok/RequestBodyProgress.java | 2 +- .../java/org/song/http/framework/ok/ResponseBodyProgress.java | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java b/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java index 4fe1d09..aeeb177 100644 --- a/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java +++ b/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java @@ -62,7 +62,7 @@ public ResponseParams P_FORM(RequestParams params, IHttpProgress hp) throws Http public ResponseParams P_BODY(RequestParams params, IHttpProgress hp) throws HttpException { HttpURLConnection conn = getHttpURLConnection(params.urlAndPath(), params.requestMethod().name(), params.headers(), params.timeOut()); writeMediaBody(conn, params.requestBody().getContentType(), params.requestBody().getContent(), hp); - return getResponse(conn, params, null); + return getResponse(conn, params, hp); } @Override @@ -189,6 +189,7 @@ private void writeMediaBody(HttpURLConnection conn, String contentType, Object c try { OutputStream os = conn.getOutputStream(); WriteHelp wh = new WriteHelp(os, hp, len); + wh.setMark("up"); if (file != null) wh.writeByFile(file); if (bytes != null) diff --git a/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java b/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java index 04efa79..b99bb14 100644 --- a/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java +++ b/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java @@ -72,7 +72,7 @@ private int read(byte[] bytes) throws IOException { if (len > 0) readCount += len; if (hp != null) - hp.onProgress(readCount, len < 0 ? readCount : allCount, ""); + hp.onProgress(readCount, len < 0 ? readCount : allCount, "down"); return len; } diff --git a/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java b/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java index 76c73a1..75bc42d 100644 --- a/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java +++ b/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java @@ -101,7 +101,7 @@ public ResponseParams P_BODY(RequestParams params, IHttpProgress hp) throws Http if (hp != null) requestBody = new RequestBodyProgress(requestBody, hp); Request request = getRequest(params, requestBody); - Response response = getResponse(editOkHttpClient(params, null), request); + Response response = getResponse(editOkHttpClient(params, hp), request); return dealResponse(params, response); } diff --git a/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java b/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java index fbe67d1..341fcdb 100644 --- a/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java +++ b/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java @@ -33,7 +33,7 @@ public RequestBodyProgress(RequestBody requestBody, IHttpProgress iHttpProgress, } public RequestBodyProgress(RequestBody requestBody, IHttpProgress iHttpProgress) { - this(requestBody, iHttpProgress, ""); + this(requestBody, iHttpProgress, "up"); } @Override diff --git a/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java b/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java index 0b5832c..0723d38 100644 --- a/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java +++ b/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java @@ -67,7 +67,7 @@ public long read(Buffer sink, long byteCount) throws IOException { //增加当前读取的字节数,如果读取完成了bytesRead会返回-1 totalBytesRead += bytesRead != -1 ? bytesRead : 0; //回调,如果contentLength()不知道长度,会返回-1 - iHttpProgress.onProgress(totalBytesRead, bytesRead == -1 ? totalBytesRead : responseBody.contentLength(), ""); + iHttpProgress.onProgress(totalBytesRead, bytesRead == -1 ? totalBytesRead : responseBody.contentLength(), "down"); return bytesRead; } }; From bbb46a423f00f2ac429d3cb230a35fff5c3ec467 Mon Sep 17 00:00:00 2001 From: QSong <764469385@qq.com> Date: Thu, 26 Nov 2020 23:05:31 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=A2=9E=E5=BC=BAurl=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../song/http/framework/RequestParams.java | 29 ++++++++++++++++--- .../java/org/song/http/framework/Utils.java | 7 +++++ 2 files changed, 32 insertions(+), 4 deletions(-) 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 bd0d5ff..41f660f 100644 --- a/qshttp/src/main/java/org/song/http/framework/RequestParams.java +++ b/qshttp/src/main/java/org/song/http/framework/RequestParams.java @@ -131,6 +131,12 @@ public String urlEncode() { sbUrl.append(urlAndPath()); if (params != null) { + String page = ""; + int pageIndex = sbUrl.lastIndexOf("#"); + if (pageIndex > 0) { + page = sbUrl.substring(pageIndex); + sbUrl.delete(pageIndex, sbUrl.length()); + } int index = sbUrl.indexOf("?"); if (index > -1) { if (index != sbUrl.length() - 1) @@ -146,25 +152,40 @@ public String urlEncode() { .append('&'); } sbUrl.deleteCharAt(sbUrl.length() - 1); + sbUrl.append(page); } return sbUrl.toString(); } /** - * 提取参数数据 构建带参数url格式 + * 提取path数据,构建带path的url */ public String urlAndPath() { - StringBuilder sbUrl = new StringBuilder(url); - if (pathParams != null) { + StringBuilder sbUrl = new StringBuilder(url); + String page = ""; + int pageIndex = sbUrl.lastIndexOf("?"); + int pageIndex2 = sbUrl.lastIndexOf("#"); + + if (pageIndex > 0) { + page = sbUrl.substring(pageIndex); + sbUrl.delete(pageIndex, sbUrl.length()); + } else if (pageIndex2 > 0) { + page = sbUrl.substring(pageIndex2); + sbUrl.delete(pageIndex2, sbUrl.length()); + } + if (sbUrl.charAt(sbUrl.length() - 1) == '/') sbUrl.deleteCharAt(sbUrl.length() - 1); for (String value : pathParams) { value = Utils.URLEncoder(value, charset); sbUrl.append("/").append(value); } + sbUrl.append(page); + return sbUrl.toString(); + } else { + return url; } - return sbUrl.toString(); } public Builder newBuild() { 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 ef7ccb0..73cb72f 100644 --- a/qshttp/src/main/java/org/song/http/framework/Utils.java +++ b/qshttp/src/main/java/org/song/http/framework/Utils.java @@ -163,6 +163,13 @@ public static String requestLog(RequestParams request) { case HEAD: case DELETE: case OPTIONS: +// StringBuilder sbUrl = new StringBuilder(); +// sbUrl.append(request.urlAndPath()).append("?"); +// for (String name : param_map.keySet()) { +// String value = String.valueOf(param_map.get(name)); +// sbUrl.append(name).append('=').append(value).append('&'); +// } +// sbUrl.deleteCharAt(sbUrl.length() - 1); log = type + "->" + request.urlEncode() + "\nHeaders->" + head_map; break; From 987edf3a562e4de5e74cc198957672628fa0929e Mon Sep 17 00:00:00 2001 From: Qsong Date: Fri, 27 Nov 2020 10:56:44 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BC=82=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/song/demo/MainActivity.java | 15 ++-- .../java/com/song/demo/MyHttpCallback.java | 43 ++++++----- .../http/framework/HttpFutureCallback.java | 71 +++++++++++++++++++ .../song/http/framework/QSHttpCallback.java | 9 +-- .../org/song/http/framework/QSHttpClient.java | 2 +- .../song/http/framework/RequestParams.java | 26 ++++++- .../song/http/framework/ResponseParams.java | 19 ++++- 7 files changed, 153 insertions(+), 32 deletions(-) create mode 100644 qshttp/src/main/java/org/song/http/framework/HttpFutureCallback.java diff --git a/app/src/main/java/com/song/demo/MainActivity.java b/app/src/main/java/com/song/demo/MainActivity.java index 8cf7ca4..614bc82 100644 --- a/app/src/main/java/com/song/demo/MainActivity.java +++ b/app/src/main/java/com/song/demo/MainActivity.java @@ -14,17 +14,17 @@ import org.song.http.QSHttp; import org.song.http.framework.HttpCallback; +import org.song.http.framework.HttpCallbackProgress; import org.song.http.framework.HttpException; import org.song.http.framework.Parser; -import org.song.http.framework.HttpCallbackProgress; import org.song.http.framework.QSHttpCallback; import org.song.http.framework.QSHttpConfig; import org.song.http.framework.ResponseParams; -import org.song.http.framework.TrustAllCerts; import java.io.File; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; public class MainActivity extends AppCompatActivity { @@ -56,8 +56,15 @@ public void onClick(View v) { downGET(url); upLoad(url); - parserJson(); +// parserJson(); + try { + tv.append(QSHttp.get(url).param("future", "future").buildAndExecute().get().string() + "成功future\n"); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } @@ -299,7 +306,7 @@ public void onSuccess(ResponseParams response) { response.headers();//获得响应头 //获得自动解析/自定义解析的结果 - User b = response.parserObject(); + User b = response.parserModel(); b.getUserName(); } diff --git a/app/src/main/java/com/song/demo/MyHttpCallback.java b/app/src/main/java/com/song/demo/MyHttpCallback.java index c3ac739..7cf94a4 100644 --- a/app/src/main/java/com/song/demo/MyHttpCallback.java +++ b/app/src/main/java/com/song/demo/MyHttpCallback.java @@ -1,18 +1,17 @@ package com.song.demo; +import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.support.annotation.CallSuper; import android.widget.Toast; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import org.song.http.framework.HttpException; import org.song.http.framework.QSHttpCallback; -import org.song.http.framework.ResponseParams; /* * Created by song on 2019/4/16. @@ -20,23 +19,29 @@ */ public abstract class MyHttpCallback extends QSHttpCallback { - @Override - public void onSuccess(ResponseParams response) { - this.response = response; - - try { - JSONObject jsonObject = JSON.parseObject(response.string(), JSONObject.class); - //服务器状态码不对 - if (jsonObject.getInteger("status") == null || jsonObject.getInteger("status") != 0) { - onFailure(HttpException.Custom(jsonObject.getString("msg"))); - return; - } + protected boolean isShow = true; - onComplete(parserT(jsonObject.get("data"))); - } catch (JSONException e) { - e.printStackTrace(); - onFailure(HttpException.Parser(e).responseParams(response)); + public MyHttpCallback() { + super(); + } + + public MyHttpCallback(boolean isShow) { + this.isShow = isShow; + } + + public MyHttpCallback(Activity activity, boolean isShow) { + super(activity); + this.isShow = isShow; + } + + @Override + public T map(String response) throws HttpException { + JSONObject jsonObject = JSON.parseObject(response, JSONObject.class); + //服务器状态码不对 + if (jsonObject.getIntValue("status") != 0) { + throw HttpException.Custom(jsonObject.getString("msg")); } + return parserT(jsonObject.getString("data")); } @@ -63,7 +68,7 @@ public void onEnd() { * 用于显示Dialog */ protected void showProgressDialog(boolean mCancelable) { - if (mDialog == null && activity != null) { + if (isShow && mDialog == null && activity != null && !activity.isFinishing()) { mDialog = new ProgressDialog(activity); mDialog.setCancelable(mCancelable); if (mCancelable) { @@ -78,7 +83,7 @@ public void onCancel(DialogInterface dialogInterface) { } protected void dismissProgressDialog() { - if (mDialog != null) { + if (isShow && mDialog != null && activity != null && !activity.isFinishing()) { mDialog.dismiss(); mDialog = null; } diff --git a/qshttp/src/main/java/org/song/http/framework/HttpFutureCallback.java b/qshttp/src/main/java/org/song/http/framework/HttpFutureCallback.java new file mode 100644 index 0000000..556ac35 --- /dev/null +++ b/qshttp/src/main/java/org/song/http/framework/HttpFutureCallback.java @@ -0,0 +1,71 @@ +package org.song.http.framework; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +/** + * Created by QSong + * Contact github.com/tohodog + * Date 2020/11/26 + * 同步 + */ +public class HttpFutureCallback implements HttpCallback, Future { + + private final CountDownLatch latch = new CountDownLatch(1); + + private ResponseParams result = null; + private Throwable error = null; + + @Override + public void onSuccess(ResponseParams response) { + this.result = response; + latch.countDown(); + } + + @Override + public void onFailure(HttpException e) { + this.error = e; + latch.countDown(); + } + + + @Override + public ResponseParams get() throws ExecutionException, InterruptedException { + latch.await(); + if (error != null) { + throw new ExecutionException(error); + } + return result; + } + + @Override + public ResponseParams get(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { + if (latch.await(timeout, unit)) { + if (error != null) { + throw new ExecutionException(error); + } + return result; + } else { + throw new TimeoutException("HttpFutureCallback get time out " + unit.toMillis(timeout)); + } + } + + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + + @Override + public boolean isDone() { + return latch.getCount() == 0; + } +} diff --git a/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java b/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java index e1d676d..ba747e6 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java @@ -45,16 +45,17 @@ public QSHttpCallback(Activity activity) { public void onSuccess(ResponseParams response) { this.response = response; try { - onComplete(parserT(response.string())); + onComplete(map(response.string())); } catch (JSONException e) { e.printStackTrace(); onFailure(HttpException.Parser(e).responseParams(response)); + } catch (HttpException e1) { + onFailure(e1.responseParams(response)); } } - protected T parserT(Object json) throws JSONException { -// return TypeUtils.cast(json, findT(), ParserConfig.global);//解析不到 - return parserT(String.valueOf(json)); + protected T map(String result) throws HttpException { + return parserT(result); } protected T parserT(String json) throws JSONException { 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 59a51af..6724b3e 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java @@ -42,7 +42,7 @@ public QSHttpClient(IHttpTask iHttpTask, QSHttpConfig qsHttpConfig) { public int execute(final RequestParams request, HttpCallback cb) { final int mThreadWhat = ThreadHandler.AddHttpCallback(cb); final boolean isProgress = cb instanceof IHttpProgress; - final boolean isSync = false; + final boolean isSync = cb instanceof HttpFutureCallback; executorService.submit(new Runnable() { 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 41f660f..196e30a 100644 --- a/qshttp/src/main/java/org/song/http/framework/RequestParams.java +++ b/qshttp/src/main/java/org/song/http/framework/RequestParams.java @@ -13,6 +13,7 @@ import java.util.IdentityHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.Future; /** * Created by song on 2016/9/18. @@ -549,6 +550,18 @@ public RequestParams.Builder jsonModel(Class _class) { return this; } + /** + * 自动解析模型 + * 返回xml解析成对象 + * 暂不支持 + */ + @Deprecated + public RequestParams.Builder xmlModel(Class _class) { + this._class = _class; + parserMode(HttpEnum.ParserMode.XML); + return this; + } + /** * 手动解析的实现类 */ @@ -597,10 +610,11 @@ public int buildAndExecute(HttpCallback cb) { return build().execute(cb); } - //构建并执行 - public int buildAndExecute() { - return buildAndExecute(null); + //构建并执行,异步转同步 + public Future buildAndExecute() { + return build().execute(); } + } public static class RequestBody { @@ -667,4 +681,10 @@ public int execute(HttpCallback cb) { return qsHttpClient.execute(this, cb); } } + + public Future execute() { + HttpFutureCallback httpFutureCallback = new HttpFutureCallback(); + execute(httpFutureCallback); + return httpFutureCallback; + } } diff --git a/qshttp/src/main/java/org/song/http/framework/ResponseParams.java b/qshttp/src/main/java/org/song/http/framework/ResponseParams.java index 5dd5fc8..3134e67 100644 --- a/qshttp/src/main/java/org/song/http/framework/ResponseParams.java +++ b/qshttp/src/main/java/org/song/http/framework/ResponseParams.java @@ -3,6 +3,9 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + import java.util.List; import java.util.Map; @@ -55,10 +58,24 @@ public byte[] bytes() { return bytes; } - public T parserObject() { + /** + * RequestParams入参配置的自动解析的模型 + */ + public T parserModel() { return (T) parserObject; } + /** + * 解析json + */ + public T jsonModel(Class _class) { + return JSON.parseObject(string, _class); + } + + public JSONObject jsonModel() { + return jsonModel(JSONObject.class); + } + public Map> headers() { return headers; } From bea2db68665aee5109609526636467a576395621 Mon Sep 17 00:00:00 2001 From: Qsong Date: Fri, 27 Nov 2020 15:42:40 +0800 Subject: [PATCH 5/6] moved pack --- .../main/java/com/song/demo/MainActivity.java | 12 ++++++------ .../main/java/com/song/demo/MyHttpCallback.java | 4 ++-- app/src/main/java/com/song/demo/NetManager.java | 3 +-- .../main/java/com/song/demo/QSInterceptor.java | 2 +- .../main/java/com/song/demo/QSInterceptor2.java | 2 +- .../org/song/http/framework/HttpException.java | 2 ++ .../song/http/framework/HttpResultHandler.java | 2 ++ .../org/song/http/framework/QSHttpClient.java | 8 ++++++++ .../org/song/http/framework/QSHttpConfig.java | 2 ++ .../org/song/http/framework/QSHttpManage.java | 1 + .../org/song/http/framework/RequestParams.java | 5 +++++ .../org/song/http/framework/ThreadHandler.java | 5 ++++- .../framework/{ => ability}/HttpCallback.java | 5 ++++- .../framework/{ => ability}/HttpCallbackEx.java | 2 +- .../{ => ability}/HttpCallbackProgress.java | 2 +- .../{ => ability}/HttpFutureCallback.java | 5 ++++- .../framework/{ => ability}/IHttpProgress.java | 2 +- .../http/framework/{ => ability}/IHttpTask.java | 6 +++++- .../framework/{ => ability}/Interceptor.java | 6 +++++- .../http/framework/{ => ability}/Parser.java | 2 +- .../framework/java/HttpURLConnectionTask.java | 6 +++--- .../song/http/framework/java/MultipartHelp.java | 4 ++-- .../org/song/http/framework/java/ReadHelp.java | 2 +- .../org/song/http/framework/java/WriteHelp.java | 2 +- .../org/song/http/framework/ok/OkHttpTask.java | 6 +++--- .../http/framework/ok/RequestBodyProgress.java | 2 +- .../http/framework/ok/ResponseBodyProgress.java | 2 +- .../http/framework/{ => util}/HttpCache.java | 4 +++- .../framework/{ => util}/QSHttpCallback.java | 16 ++++++++++++++-- .../http/framework/{ => util}/TrustAllCerts.java | 2 +- .../song/http/framework/{ => util}/Utils.java | 12 +++++++++--- 31 files changed, 96 insertions(+), 40 deletions(-) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/HttpCallback.java (54%) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/HttpCallbackEx.java (82%) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/HttpCallbackProgress.java (74%) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/HttpFutureCallback.java (92%) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/IHttpProgress.java (81%) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/IHttpTask.java (86%) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/Interceptor.java (63%) rename qshttp/src/main/java/org/song/http/framework/{ => ability}/Parser.java (74%) rename qshttp/src/main/java/org/song/http/framework/{ => util}/HttpCache.java (97%) rename qshttp/src/main/java/org/song/http/framework/{ => util}/QSHttpCallback.java (89%) rename qshttp/src/main/java/org/song/http/framework/{ => util}/TrustAllCerts.java (97%) rename qshttp/src/main/java/org/song/http/framework/{ => util}/Utils.java (98%) diff --git a/app/src/main/java/com/song/demo/MainActivity.java b/app/src/main/java/com/song/demo/MainActivity.java index 614bc82..bc7551b 100644 --- a/app/src/main/java/com/song/demo/MainActivity.java +++ b/app/src/main/java/com/song/demo/MainActivity.java @@ -13,11 +13,11 @@ import com.alibaba.fastjson.JSONObject; import org.song.http.QSHttp; -import org.song.http.framework.HttpCallback; -import org.song.http.framework.HttpCallbackProgress; +import org.song.http.framework.ability.HttpCallback; +import org.song.http.framework.ability.HttpCallbackProgress; import org.song.http.framework.HttpException; -import org.song.http.framework.Parser; -import org.song.http.framework.QSHttpCallback; +import org.song.http.framework.ability.Parser; +import org.song.http.framework.util.QSHttpCallback; import org.song.http.framework.QSHttpConfig; import org.song.http.framework.ResponseParams; @@ -72,7 +72,7 @@ public void onClick(View v) { public void normalGET(String url) { QSHttp.get(url) - .param("wd", "安卓http") + .param("wd", "Android") .param("ie", "UTF-8")//自动构建url--https://www.baidu.com/s?ie=UTF-8&wd=安卓http //.path(123,11) 这个参数会构建这样的url--https://www.baidu.com/s/123/11 .buildAndExecute(new HttpCallback() { @@ -92,7 +92,7 @@ public void onFailure(HttpException e) { public void normalPost(String url) { QSHttp.post(url) .param("userid", 10086) - .param("password", "qwe123456对") + .param("password", "安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp") .buildAndExecute(new MyHttpCallback() { @Override public void onComplete(JSONObject dataBean) { diff --git a/app/src/main/java/com/song/demo/MyHttpCallback.java b/app/src/main/java/com/song/demo/MyHttpCallback.java index 7cf94a4..9afa755 100644 --- a/app/src/main/java/com/song/demo/MyHttpCallback.java +++ b/app/src/main/java/com/song/demo/MyHttpCallback.java @@ -11,7 +11,7 @@ import com.alibaba.fastjson.JSONObject; import org.song.http.framework.HttpException; -import org.song.http.framework.QSHttpCallback; +import org.song.http.framework.util.QSHttpCallback; /* * Created by song on 2019/4/16. @@ -36,7 +36,7 @@ public MyHttpCallback(Activity activity, boolean isShow) { @Override public T map(String response) throws HttpException { - JSONObject jsonObject = JSON.parseObject(response, JSONObject.class); + JSONObject jsonObject = JSON.parseObject(response); //服务器状态码不对 if (jsonObject.getIntValue("status") != 0) { throw HttpException.Custom(jsonObject.getString("msg")); diff --git a/app/src/main/java/com/song/demo/NetManager.java b/app/src/main/java/com/song/demo/NetManager.java index a5a6697..9631d35 100644 --- a/app/src/main/java/com/song/demo/NetManager.java +++ b/app/src/main/java/com/song/demo/NetManager.java @@ -13,9 +13,8 @@ import android.widget.Toast; import org.song.http.QSHttp; -import org.song.http.framework.HttpEnum; import org.song.http.framework.QSHttpConfig; -import org.song.http.framework.TrustAllCerts; +import org.song.http.framework.util.TrustAllCerts; import java.io.IOException; diff --git a/app/src/main/java/com/song/demo/QSInterceptor.java b/app/src/main/java/com/song/demo/QSInterceptor.java index 4d7ca94..a390aae 100644 --- a/app/src/main/java/com/song/demo/QSInterceptor.java +++ b/app/src/main/java/com/song/demo/QSInterceptor.java @@ -1,7 +1,7 @@ package com.song.demo; import org.song.http.framework.HttpException; -import org.song.http.framework.Interceptor; +import org.song.http.framework.ability.Interceptor; import org.song.http.framework.RequestParams; import org.song.http.framework.ResponseParams; diff --git a/app/src/main/java/com/song/demo/QSInterceptor2.java b/app/src/main/java/com/song/demo/QSInterceptor2.java index 3b08530..2b78367 100644 --- a/app/src/main/java/com/song/demo/QSInterceptor2.java +++ b/app/src/main/java/com/song/demo/QSInterceptor2.java @@ -1,7 +1,7 @@ package com.song.demo; import org.song.http.framework.HttpException; -import org.song.http.framework.Interceptor; +import org.song.http.framework.ability.Interceptor; import org.song.http.framework.RequestParams; import org.song.http.framework.ResponseParams; diff --git a/qshttp/src/main/java/org/song/http/framework/HttpException.java b/qshttp/src/main/java/org/song/http/framework/HttpException.java index ae63eba..8cc265b 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpException.java +++ b/qshttp/src/main/java/org/song/http/framework/HttpException.java @@ -1,5 +1,7 @@ package org.song.http.framework; +import org.song.http.framework.util.Utils; + import java.util.Locale; /** diff --git a/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java b/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java index 3935eb5..7c44524 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java +++ b/qshttp/src/main/java/org/song/http/framework/HttpResultHandler.java @@ -2,6 +2,8 @@ import com.alibaba.fastjson.JSON; +import org.song.http.framework.util.HttpCache; + /** * Created by song on 2016/9/18. */ 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 6724b3e..8845736 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpClient.java @@ -1,6 +1,14 @@ package org.song.http.framework; +import org.song.http.framework.ability.HttpCallback; +import org.song.http.framework.ability.HttpFutureCallback; +import org.song.http.framework.ability.IHttpProgress; +import org.song.http.framework.ability.IHttpTask; +import org.song.http.framework.ability.Interceptor; +import org.song.http.framework.util.HttpCache; +import org.song.http.framework.util.Utils; + import java.util.List; import java.util.Timer; import java.util.TimerTask; 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 bfc569a..c5666c4 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpConfig.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpConfig.java @@ -4,6 +4,8 @@ import android.net.Network; import android.os.Build; +import org.song.http.framework.ability.Interceptor; + import java.util.ArrayList; import java.util.List; diff --git a/qshttp/src/main/java/org/song/http/framework/QSHttpManage.java b/qshttp/src/main/java/org/song/http/framework/QSHttpManage.java index e6dedfa..ef33be3 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpManage.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpManage.java @@ -5,6 +5,7 @@ import org.song.http.framework.java.JavaHttpClient; import org.song.http.framework.ok.OkHttpClient; +import org.song.http.framework.util.Utils; import java.util.HashMap; import java.util.Map; 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 196e30a..027f1be 100644 --- a/qshttp/src/main/java/org/song/http/framework/RequestParams.java +++ b/qshttp/src/main/java/org/song/http/framework/RequestParams.java @@ -5,6 +5,11 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import org.song.http.framework.ability.HttpCallback; +import org.song.http.framework.ability.HttpFutureCallback; +import org.song.http.framework.ability.Parser; +import org.song.http.framework.util.Utils; + import java.io.File; import java.nio.charset.Charset; import java.util.ArrayList; diff --git a/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java b/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java index e0b717e..e355e34 100644 --- a/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java +++ b/qshttp/src/main/java/org/song/http/framework/ThreadHandler.java @@ -3,7 +3,10 @@ import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.util.SparseArray; + +import org.song.http.framework.ability.HttpCallback; +import org.song.http.framework.ability.HttpCallbackEx; +import org.song.http.framework.ability.IHttpProgress; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; diff --git a/qshttp/src/main/java/org/song/http/framework/HttpCallback.java b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallback.java similarity index 54% rename from qshttp/src/main/java/org/song/http/framework/HttpCallback.java rename to qshttp/src/main/java/org/song/http/framework/ability/HttpCallback.java index c63c7fc..7ede50f 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpCallback.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallback.java @@ -1,4 +1,7 @@ -package org.song.http.framework; +package org.song.http.framework.ability; + +import org.song.http.framework.HttpException; +import org.song.http.framework.ResponseParams; /** * Created by song on 2016/9/18. diff --git a/qshttp/src/main/java/org/song/http/framework/HttpCallbackEx.java b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackEx.java similarity index 82% rename from qshttp/src/main/java/org/song/http/framework/HttpCallbackEx.java rename to qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackEx.java index 9b583ec..f945ebe 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpCallbackEx.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackEx.java @@ -1,4 +1,4 @@ -package org.song.http.framework; +package org.song.http.framework.ability; /** * Created by song on 2017/4/13. diff --git a/qshttp/src/main/java/org/song/http/framework/HttpCallbackProgress.java b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackProgress.java similarity index 74% rename from qshttp/src/main/java/org/song/http/framework/HttpCallbackProgress.java rename to qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackProgress.java index 0ee716f..853895e 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpCallbackProgress.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackProgress.java @@ -1,4 +1,4 @@ -package org.song.http.framework; +package org.song.http.framework.ability; /** * Created by song on 2016/9/18. diff --git a/qshttp/src/main/java/org/song/http/framework/HttpFutureCallback.java b/qshttp/src/main/java/org/song/http/framework/ability/HttpFutureCallback.java similarity index 92% rename from qshttp/src/main/java/org/song/http/framework/HttpFutureCallback.java rename to qshttp/src/main/java/org/song/http/framework/ability/HttpFutureCallback.java index 556ac35..f07ad06 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpFutureCallback.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/HttpFutureCallback.java @@ -1,4 +1,7 @@ -package org.song.http.framework; +package org.song.http.framework.ability; + +import org.song.http.framework.HttpException; +import org.song.http.framework.ResponseParams; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; diff --git a/qshttp/src/main/java/org/song/http/framework/IHttpProgress.java b/qshttp/src/main/java/org/song/http/framework/ability/IHttpProgress.java similarity index 81% rename from qshttp/src/main/java/org/song/http/framework/IHttpProgress.java rename to qshttp/src/main/java/org/song/http/framework/ability/IHttpProgress.java index 0ab205d..f30cc3e 100644 --- a/qshttp/src/main/java/org/song/http/framework/IHttpProgress.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/IHttpProgress.java @@ -1,4 +1,4 @@ -package org.song.http.framework; +package org.song.http.framework.ability; /** * Created by song on 2016/9/18. diff --git a/qshttp/src/main/java/org/song/http/framework/IHttpTask.java b/qshttp/src/main/java/org/song/http/framework/ability/IHttpTask.java similarity index 86% rename from qshttp/src/main/java/org/song/http/framework/IHttpTask.java rename to qshttp/src/main/java/org/song/http/framework/ability/IHttpTask.java index 424fc95..9b957b4 100644 --- a/qshttp/src/main/java/org/song/http/framework/IHttpTask.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/IHttpTask.java @@ -1,4 +1,8 @@ -package org.song.http.framework; +package org.song.http.framework.ability; + +import org.song.http.framework.HttpException; +import org.song.http.framework.RequestParams; +import org.song.http.framework.ResponseParams; /** * Created by song on 2016/9/14. diff --git a/qshttp/src/main/java/org/song/http/framework/Interceptor.java b/qshttp/src/main/java/org/song/http/framework/ability/Interceptor.java similarity index 63% rename from qshttp/src/main/java/org/song/http/framework/Interceptor.java rename to qshttp/src/main/java/org/song/http/framework/ability/Interceptor.java index ea29f10..45adb44 100644 --- a/qshttp/src/main/java/org/song/http/framework/Interceptor.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/Interceptor.java @@ -1,4 +1,8 @@ -package org.song.http.framework; +package org.song.http.framework.ability; + +import org.song.http.framework.HttpException; +import org.song.http.framework.RequestParams; +import org.song.http.framework.ResponseParams; /** * Created by song on 2017/5/17. diff --git a/qshttp/src/main/java/org/song/http/framework/Parser.java b/qshttp/src/main/java/org/song/http/framework/ability/Parser.java similarity index 74% rename from qshttp/src/main/java/org/song/http/framework/Parser.java rename to qshttp/src/main/java/org/song/http/framework/ability/Parser.java index 22c0531..1c82a08 100644 --- a/qshttp/src/main/java/org/song/http/framework/Parser.java +++ b/qshttp/src/main/java/org/song/http/framework/ability/Parser.java @@ -1,4 +1,4 @@ -package org.song.http.framework; +package org.song.http.framework.ability; /** * Created by song on 2016/9/19. diff --git a/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java b/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java index aeeb177..17df4a6 100644 --- a/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java +++ b/qshttp/src/main/java/org/song/http/framework/java/HttpURLConnectionTask.java @@ -4,12 +4,12 @@ import org.song.http.framework.HttpEnum; import org.song.http.framework.HttpException; -import org.song.http.framework.IHttpProgress; -import org.song.http.framework.IHttpTask; +import org.song.http.framework.ability.IHttpProgress; +import org.song.http.framework.ability.IHttpTask; import org.song.http.framework.QSHttpConfig; import org.song.http.framework.RequestParams; import org.song.http.framework.ResponseParams; -import org.song.http.framework.Utils; +import org.song.http.framework.util.Utils; import java.io.File; import java.io.IOException; diff --git a/qshttp/src/main/java/org/song/http/framework/java/MultipartHelp.java b/qshttp/src/main/java/org/song/http/framework/java/MultipartHelp.java index d5221c9..730111b 100644 --- a/qshttp/src/main/java/org/song/http/framework/java/MultipartHelp.java +++ b/qshttp/src/main/java/org/song/http/framework/java/MultipartHelp.java @@ -1,9 +1,9 @@ package org.song.http.framework.java; import org.song.http.framework.HttpEnum; -import org.song.http.framework.IHttpProgress; +import org.song.http.framework.ability.IHttpProgress; import org.song.http.framework.RequestParams; -import org.song.http.framework.Utils; +import org.song.http.framework.util.Utils; import java.io.File; import java.io.IOException; diff --git a/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java b/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java index b99bb14..fe04f23 100644 --- a/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java +++ b/qshttp/src/main/java/org/song/http/framework/java/ReadHelp.java @@ -1,6 +1,6 @@ package org.song.http.framework.java; -import org.song.http.framework.IHttpProgress; +import org.song.http.framework.ability.IHttpProgress; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; diff --git a/qshttp/src/main/java/org/song/http/framework/java/WriteHelp.java b/qshttp/src/main/java/org/song/http/framework/java/WriteHelp.java index 1c4bd37..23873b5 100644 --- a/qshttp/src/main/java/org/song/http/framework/java/WriteHelp.java +++ b/qshttp/src/main/java/org/song/http/framework/java/WriteHelp.java @@ -1,6 +1,6 @@ package org.song.http.framework.java; -import org.song.http.framework.IHttpProgress; +import org.song.http.framework.ability.IHttpProgress; import java.io.BufferedInputStream; import java.io.File; diff --git a/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java b/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java index 75bc42d..d6bf0de 100644 --- a/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java +++ b/qshttp/src/main/java/org/song/http/framework/ok/OkHttpTask.java @@ -5,13 +5,13 @@ import org.song.http.framework.HttpEnum; import org.song.http.framework.HttpException; -import org.song.http.framework.IHttpProgress; -import org.song.http.framework.IHttpTask; +import org.song.http.framework.ability.IHttpProgress; +import org.song.http.framework.ability.IHttpTask; import org.song.http.framework.QSHttpConfig; import org.song.http.framework.QSHttpManage; import org.song.http.framework.RequestParams; import org.song.http.framework.ResponseParams; -import org.song.http.framework.Utils; +import org.song.http.framework.util.Utils; import org.song.http.framework.ok.cookie.CookieManage; import java.io.File; diff --git a/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java b/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java index 341fcdb..8d777fb 100644 --- a/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java +++ b/qshttp/src/main/java/org/song/http/framework/ok/RequestBodyProgress.java @@ -1,6 +1,6 @@ package org.song.http.framework.ok; -import org.song.http.framework.IHttpProgress; +import org.song.http.framework.ability.IHttpProgress; import java.io.IOException; diff --git a/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java b/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java index 0723d38..9bab908 100644 --- a/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java +++ b/qshttp/src/main/java/org/song/http/framework/ok/ResponseBodyProgress.java @@ -1,6 +1,6 @@ package org.song.http.framework.ok; -import org.song.http.framework.IHttpProgress; +import org.song.http.framework.ability.IHttpProgress; import java.io.IOException; diff --git a/qshttp/src/main/java/org/song/http/framework/HttpCache.java b/qshttp/src/main/java/org/song/http/framework/util/HttpCache.java similarity index 97% rename from qshttp/src/main/java/org/song/http/framework/HttpCache.java rename to qshttp/src/main/java/org/song/http/framework/util/HttpCache.java index fffdcf1..656211d 100644 --- a/qshttp/src/main/java/org/song/http/framework/HttpCache.java +++ b/qshttp/src/main/java/org/song/http/framework/util/HttpCache.java @@ -1,8 +1,10 @@ -package org.song.http.framework; +package org.song.http.framework.util; import android.util.Log; import org.song.http.framework.HttpEnum.CacheMode; +import org.song.http.framework.RequestParams; +import org.song.http.framework.ResponseParams; import java.io.File; import java.io.UnsupportedEncodingException; diff --git a/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java b/qshttp/src/main/java/org/song/http/framework/util/QSHttpCallback.java similarity index 89% rename from qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java rename to qshttp/src/main/java/org/song/http/framework/util/QSHttpCallback.java index ba747e6..a66e542 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java +++ b/qshttp/src/main/java/org/song/http/framework/util/QSHttpCallback.java @@ -1,4 +1,4 @@ -package org.song.http.framework; +package org.song.http.framework.util; import android.app.Activity; import android.app.Fragment; @@ -10,6 +10,12 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; +import org.song.http.framework.HttpEnum; +import org.song.http.framework.HttpException; +import org.song.http.framework.ResponseParams; +import org.song.http.framework.ability.HttpCallbackEx; + +import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; @@ -45,7 +51,13 @@ public QSHttpCallback(Activity activity) { public void onSuccess(ResponseParams response) { this.response = response; try { - onComplete(map(response.string())); + if (response.resultType() == HttpEnum.ResultType.STRING) { + onComplete(map(response.string())); + } else if (response.resultType() == HttpEnum.ResultType.BYTES) { + onComplete((T) response.bytes()); + } else if (response.resultType() == HttpEnum.ResultType.FILE) { + onComplete((T) new File(response.file())); + } } catch (JSONException e) { e.printStackTrace(); onFailure(HttpException.Parser(e).responseParams(response)); diff --git a/qshttp/src/main/java/org/song/http/framework/TrustAllCerts.java b/qshttp/src/main/java/org/song/http/framework/util/TrustAllCerts.java similarity index 97% rename from qshttp/src/main/java/org/song/http/framework/TrustAllCerts.java rename to qshttp/src/main/java/org/song/http/framework/util/TrustAllCerts.java index ef3292a..20d20a6 100644 --- a/qshttp/src/main/java/org/song/http/framework/TrustAllCerts.java +++ b/qshttp/src/main/java/org/song/http/framework/util/TrustAllCerts.java @@ -1,4 +1,4 @@ -package org.song.http.framework; +package org.song.http.framework.util; import java.security.SecureRandom; diff --git a/qshttp/src/main/java/org/song/http/framework/Utils.java b/qshttp/src/main/java/org/song/http/framework/util/Utils.java similarity index 98% rename from qshttp/src/main/java/org/song/http/framework/Utils.java rename to qshttp/src/main/java/org/song/http/framework/util/Utils.java index 73cb72f..36f9f15 100644 --- a/qshttp/src/main/java/org/song/http/framework/Utils.java +++ b/qshttp/src/main/java/org/song/http/framework/util/Utils.java @@ -1,4 +1,4 @@ -package org.song.http.framework; +package org.song.http.framework.util; import android.Manifest; import android.content.Context; @@ -12,6 +12,12 @@ import android.util.Log; import android.widget.Toast; +import org.song.http.framework.HttpEnum; +import org.song.http.framework.QSHttpConfig; +import org.song.http.framework.QSHttpManage; +import org.song.http.framework.RequestParams; +import org.song.http.framework.ResponseParams; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -146,8 +152,8 @@ public static SSLSocketFactory checkSSL(String host, QSHttpConfig qsHttpConfig) public static void Log(RequestParams request, ResponseParams response, long time) { String requestLog = requestLog(request); String responseLog = responseLog(response); - String log = requestLog + "\n请求结果" + time + "ms-> ↓↓↓\n" + responseLog; - Log.e(TAG, log); + String log = requestLog + "\n请求结果" + time + "ms-> ↓↓↓\n" + responseLog+"\n "; + Log.d(TAG, log); } From 112b6eed3fbdee22723039eb423fa6b1944c07f4 Mon Sep 17 00:00:00 2001 From: Qsong Date: Fri, 27 Nov 2020 18:42:18 +0800 Subject: [PATCH 6/6] 1.5.3 --- README.md | 4 +- .../com/song/demo/{User.java => Bean.java} | 2 +- .../main/java/com/song/demo/MainActivity.java | 205 +++++++++--------- .../java/com/song/demo/MyHttpCallback.java | 3 +- qshttp/build.gradle | 4 +- .../ability/HttpCallbackProgressEx.java | 8 + 6 files changed, 123 insertions(+), 103 deletions(-) rename app/src/main/java/com/song/demo/{User.java => Bean.java} (90%) create mode 100644 qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackProgressEx.java diff --git a/README.md b/README.md index e890291..db1a20a 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ https://api.reol.top/api_test .param("ie", "UTF-8") .buildAndExecute(new QSHttpCallback() { @Override - public void onComplete(Bean dataUser) { + public void onComplete(Bean dataBean) { } //@Override @@ -110,7 +110,7 @@ https://api.reol.top/api_test .jsonModel(Bean.class) .buildAndExecute(new QSHttpCallback() { @Override - public void onComplete(UserBean dataUser) { + public void onComplete(UserBean dataBean) { } }); diff --git a/app/src/main/java/com/song/demo/User.java b/app/src/main/java/com/song/demo/Bean.java similarity index 90% rename from app/src/main/java/com/song/demo/User.java rename to app/src/main/java/com/song/demo/Bean.java index b4c3f5f..9143c15 100644 --- a/app/src/main/java/com/song/demo/User.java +++ b/app/src/main/java/com/song/demo/Bean.java @@ -7,7 +7,7 @@ * Created by song on 2017/2/16. */ -public class User implements Serializable { +public class Bean implements Serializable { private String userName; diff --git a/app/src/main/java/com/song/demo/MainActivity.java b/app/src/main/java/com/song/demo/MainActivity.java index bc7551b..87eb5a5 100644 --- a/app/src/main/java/com/song/demo/MainActivity.java +++ b/app/src/main/java/com/song/demo/MainActivity.java @@ -9,21 +9,19 @@ import android.widget.ImageView; import android.widget.TextView; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.song.http.QSHttp; +import org.song.http.framework.HttpException; +import org.song.http.framework.QSHttpConfig; +import org.song.http.framework.ResponseParams; import org.song.http.framework.ability.HttpCallback; +import org.song.http.framework.ability.HttpCallbackEx; import org.song.http.framework.ability.HttpCallbackProgress; -import org.song.http.framework.HttpException; import org.song.http.framework.ability.Parser; import org.song.http.framework.util.QSHttpCallback; -import org.song.http.framework.QSHttpConfig; -import org.song.http.framework.ResponseParams; import java.io.File; -import java.util.Arrays; -import java.util.List; import java.util.concurrent.ExecutionException; public class MainActivity extends AppCompatActivity { @@ -71,6 +69,16 @@ public void onClick(View v) { //普通带参数 get public void normalGET(String url) { + QSHttp.get(url) + .param("name", "QSHttp") + .buildAndExecute(new QSHttpCallback() { + @Override + public void onComplete(Bean bean) { + + } + }); + + QSHttp.get(url) .param("wd", "Android") .param("ie", "UTF-8")//自动构建url--https://www.baidu.com/s?ie=UTF-8&wd=安卓http @@ -86,13 +94,15 @@ public void onFailure(HttpException e) { e.show(); } }); + + } //普通键值对 post, application/x-www-form-urlencoded public void normalPost(String url) { QSHttp.post(url) .param("userid", 10086) - .param("password", "安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp安卓htt安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓http安卓httpp") + .param("password", "qwe123456") .buildAndExecute(new MyHttpCallback() { @Override public void onComplete(JSONObject dataBean) { @@ -191,110 +201,40 @@ public void onFailure(HttpException e) { } - private void parserJson() { - - User dataUser = new User(); - User dataUser2 = new User(); - dataUser.setUserName("Yolanda"); - dataUser2.setUserName("Song"); - List users = Arrays.asList(dataUser2); - dataUser.setRows(users); - - - QSHttp.postJSON("https://api.reol.top/test/json") - .jsonBody(dataUser) - .buildAndExecute(new MyHttpCallback>() { - @Override - public void onComplete(User dataUser) { - tv.append("MyHttpCallback.User=" + JSON.toJSONString(dataUser) + dataUser.getRows().get(0).getClass() + "\n"); - } - }); - - QSHttp.postJSON("https://api.reol.top/test/json") - .jsonBody("3.666489") - .buildAndExecute(new MyHttpCallback() { - @Override - public void onComplete(Double dataUser) { - tv.append("MyHttpCallback.Double=" + JSON.toJSONString(dataUser) + "\n"); - } - }); - - QSHttp.postJSON("https://api.reol.top/test/json") - .header("row", "row") - .jsonBody(users) - .buildAndExecute(new QSHttpCallback>() { - @Override - public void onComplete(List dataUser) { - tv.append("QSHttpCallback.List=" + JSON.toJSONString(dataUser) + dataUser.get(0).getClass() + "\n"); - } - }); - - QSHttp.postJSON("https://api.reol.top/test/json") - .header("row", "row") - .jsonBody(dataUser) - .buildAndExecute(new QSHttpCallback() { - @Override - public void onComplete(User dataUser) { - tv.append("QSHttpCallback.User=" + JSON.toJSONString(dataUser) + dataUser.getClass() + "\n"); - } - }); - - QSHttp.postJSON("https://api.reol.top/test/json") - .header("row", "row") - .jsonBody("3.6") - .buildAndExecute(new QSHttpCallback() { - @Override - public void onComplete(String dataUser) { - tv.append("QSHttpCallback.String=" + dataUser + "\n"); - } - }); - - } - - //基本所有api介绍 public void allAPI() { - String url = "https://www.baidu.com/s"; + String url = "https://api.reol.top/api_test"; QSHttp.post(url)//选择请求的类型 .header("User-Agent", "QsHttp/Android")//添加请求头 - .path(2333, "video")//构建成这样的url https://www.baidu.com/s/2233/video + .path(2333, "video")//构建成这样的url https://api.reol.top/api_test/2233/video - .param("userid", 123456)//键值对参数 + .param("userName", 123456)//键值对参数 .param("password", "asdfgh")//键值对参数 - .param(new User())//键值对参数 - - .toJsonBody()//把 params 转为json;application/json - .jsonBody(new User())//传入一个对象,会自动转化为json上传;application/json - - .requestBody("image/jpeg", new File("xx.jpg"))//直接上传自定义的内容 自定义contentType (postjson内部是调用这个实现) - + .param(new Bean())//键值对参数 .param("bytes", new byte[1024])//传一个字节数组,multipart支持此参数 .param("file", new File("xx.jpg"))//传一个文件,multipart支持此参数 - .toMultiBody()//把 params 转为multipartBody参数;multipart/form-data + .jsonBody(new Bean())//传入一个对象,会自动转化为json上传;application/json + //自定义Body的内容 自定义contentType (postjson内部是调用这个实现) + .requestBody("image/jpeg", new File("xx.jpg")) .parser(parser)//自定义解析,由自己写解析逻辑 - .jsonModel(User.class)//使用FastJson自动解析json,传一个实体类即可 - .resultByBytes()//请求结果返回一个字节组 默认是返回字符 .resultByFile(".../1.txt")//本地路径 有此参数 请求的内容将被写入文件 .errCache()//开启这个 [联网失败]会使用缓存,如果有的话 - .clientCache(24 * 3600)//开启缓存,有效时间一天 - .timeOut(10 * 1000) + .timeOut(10 * 1000)//单独设置超时 .openServerCache()//开启服务器缓存规则 基于okhttp支持 - - //构建好参数和配置后调用执行联网 - .buildAndExecute(new HttpCallbackProgress() { - - //-----回调均已在主线程 + .clientCache(24 * 3600)//开启强制缓存,一天内都不会请求了 + .charset("utf-8")//特殊需求可以更改编码 + .tag("no token")//可配合拦截器使用 + //执行联网 + .buildAndExecute(new HttpCallbackEx() { @Override - public void onProgress(long var1, long var2, String var3) { - //进度回调 不需要监听进度 buildAndExecute()传 new HttpCallback(){...}即可 - long i = var1 * 100 / var2;//百分比 - //var3 在传文件的时候为文件路径 其他无意义 + public void onStart() { + //开始请求 } @Override @@ -305,14 +245,25 @@ public void onSuccess(ResponseParams response) { response.headers();//获得响应头 - //获得自动解析/自定义解析的结果 - User b = response.parserModel(); - b.getUserName(); + //获得解析的模型 + Bean b = response.jsonModel(Bean.class); } @Override public void onFailure(HttpException e) { e.show();//弹出错误提示 网络连接失败 超时 404 解析失败 ...等 + String response = (String) e.getExObject();//可获取非200异常的参数 + } + + @Override + public void onEnd() { + //结束请求 + } + + + @Override + public boolean isDestroy() { + return isFinishing();//页面关闭不会回调 } }); @@ -329,11 +280,71 @@ public void onFailure(HttpException e) { } - Parser parser = new Parser() { + Parser parser = new Parser() { @Override - public User parser(String result) throws Exception { + public Bean parser(String result) throws Exception { return null; } }; + + +// private void parserJson() { +// +// Bean dataBean = new Bean(); +// Bean dataBean2 = new Bean(); +// dataBean.setUserName("Yolanda"); +// dataBean2.setUserName("Song"); +// List beans = Arrays.asList(dataBean2); +// dataBean.setRows(beans); +// +// +// QSHttp.postJSON("https://api.reol.top/test/json") +// .jsonBody(dataBean) +// .buildAndExecute(new MyHttpCallback>() { +// @Override +// public void onComplete(Bean dataBean) { +// tv.append("MyHttpCallback.User=" + JSON.toJSONString(dataBean) + dataBean.getRows().get(0).getClass() + "\n"); +// } +// }); +// +// QSHttp.postJSON("https://api.reol.top/test/json") +// .jsonBody("3.666489") +// .buildAndExecute(new MyHttpCallback() { +// @Override +// public void onComplete(Double dataUser) { +// tv.append("MyHttpCallback.Double=" + JSON.toJSONString(dataUser) + "\n"); +// } +// }); +// +// QSHttp.postJSON("https://api.reol.top/test/json") +// .header("row", "row") +// .jsonBody(beans) +// .buildAndExecute(new QSHttpCallback>() { +// @Override +// public void onComplete(List dataBean) { +// tv.append("QSHttpCallback.List=" + JSON.toJSONString(dataBean) + dataBean.get(0).getClass() + "\n"); +// } +// }); +// +// QSHttp.postJSON("https://api.reol.top/test/json") +// .header("row", "row") +// .jsonBody(dataBean) +// .buildAndExecute(new QSHttpCallback() { +// @Override +// public void onComplete(Bean dataBean) { +// tv.append("QSHttpCallback.User=" + JSON.toJSONString(dataBean) + dataBean.getClass() + "\n"); +// } +// }); +// +// QSHttp.postJSON("https://api.reol.top/test/json") +// .header("row", "row") +// .jsonBody("3.6") +// .buildAndExecute(new QSHttpCallback() { +// @Override +// public void onComplete(String dataUser) { +// tv.append("QSHttpCallback.String=" + dataUser + "\n"); +// } +// }); +// } } diff --git a/app/src/main/java/com/song/demo/MyHttpCallback.java b/app/src/main/java/com/song/demo/MyHttpCallback.java index 9afa755..d63ee22 100644 --- a/app/src/main/java/com/song/demo/MyHttpCallback.java +++ b/app/src/main/java/com/song/demo/MyHttpCallback.java @@ -39,7 +39,7 @@ public T map(String response) throws HttpException { JSONObject jsonObject = JSON.parseObject(response); //服务器状态码不对 if (jsonObject.getIntValue("status") != 0) { - throw HttpException.Custom(jsonObject.getString("msg")); + throw HttpException.Custom(jsonObject.getString("msg"), response); } return parserT(jsonObject.getString("data")); } @@ -47,6 +47,7 @@ public T map(String response) throws HttpException { @Override public void onFailure(HttpException e) { + String response = (String) e.getExObject();//可获取非200异常的参数 if (activity != null) Toast.makeText(activity, e.getPrompt(), Toast.LENGTH_LONG).show(); } diff --git a/qshttp/build.gradle b/qshttp/build.gradle index 763fe77..89bce7e 100644 --- a/qshttp/build.gradle +++ b/qshttp/build.gradle @@ -6,8 +6,8 @@ android { minSdkVersion 9 targetSdkVersion compileSdkVersion 28 - versionCode 14 - versionName "1.5.2" + versionCode 15 + versionName "1.5.3" } buildTypes { release { diff --git a/qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackProgressEx.java b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackProgressEx.java new file mode 100644 index 0000000..70e6d85 --- /dev/null +++ b/qshttp/src/main/java/org/song/http/framework/ability/HttpCallbackProgressEx.java @@ -0,0 +1,8 @@ +package org.song.http.framework.ability; + +/** + * Created by song on 2020/11/28. + */ +public interface HttpCallbackProgressEx extends HttpCallbackEx, IHttpProgress { + +}