From 112b6eed3fbdee22723039eb423fa6b1944c07f4 Mon Sep 17 00:00:00 2001 From: Qsong Date: Fri, 27 Nov 2020 18:42:18 +0800 Subject: [PATCH] 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 { + +}