diff --git a/README.md b/README.md index 3edae84..3fd094c 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ QSHttp ### Gradle ``` dependencies { - implementation 'com.github.tohodog:QSHttp:1.4.2' + implementation 'com.github.tohodog:QSHttp:1.4.3' } ``` @@ -57,7 +57,7 @@ https://api.reol.top/api_test ``` String url = "https://api.reol.top/api_test"; QSHttp.post(url) - .param("userid", 10086) + .param("userName", 10086) .param("password", "qwe123456") .buildAndExecute(new HttpCallback() { @Override @@ -76,7 +76,7 @@ https://api.reol.top/api_test ``` String url = "https://api.reol.top/api_test"; QSHttp.postJSON(url) - .param("userid", 10086) + .param("userName", 10086) .param("password", "qwe123456") //.jsonBody(Object) 这个参数可以直接传一个实体类,fastjson会自动转化成json字符串 .jsonModel(Bean.class) @@ -95,7 +95,7 @@ https://api.reol.top/api_test }); //使用QSHttpCallback,支持外部类是activity,fragment时销毁不回调 QSHttp.postJSON(url) - .param("userid", 10086) + .param("userName", 10086) .param("password", "qwe123456") .buildAndExecute(new QSHttpCallback() { @Override @@ -135,7 +135,7 @@ https://api.reol.top/api_test ``` String url = "https://api.reol.top/api_test"; QSHttp.upload(url) - .param("userid", 10086) + .param("userName", 10086) .param("password", "qwe123456") .param("bytes", new byte[1024])//multipart方式上传一个字节数组 @@ -212,7 +212,7 @@ https://api.reol.top/api_test .path(2333, "video")//构建成这样的url https://api.reol.top/api_test/2233/video - .param("userid", 123456)//键值对参数 + .param("userName", 123456)//键值对参数 .param("password", "asdfgh")//键值对参数 .param(new Bean())//键值对参数 @@ -268,6 +268,8 @@ https://api.reol.top/api_test }); ``` ## Log +### v1.4.3(2019-07-18) + * 优化(泛型) ### v1.4.2(2019-06-27) * 优化(进度监听,泛型) ### v1.4.1(2019-05-30) diff --git a/app/src/main/java/com/song/demo/MainActivity.java b/app/src/main/java/com/song/demo/MainActivity.java index 099a73b..018cb06 100644 --- a/app/src/main/java/com/song/demo/MainActivity.java +++ b/app/src/main/java/com/song/demo/MainActivity.java @@ -65,7 +65,7 @@ protected void onCreate(Bundle savedInstanceState) { downGET(url); upLoad(url); - parserJson(); +// parserJson(); } @@ -123,7 +123,7 @@ public void jsonPost(String url) { public void onSuccess(ResponseParams response) { tv.append(response.requestParams().url() + "成功postJSON\n"); // User b = response.parserObject();//解析好的模型 -// b.getUserid(); +// b.getUserName(); } @Override @@ -142,7 +142,7 @@ public void onFailure(HttpException e) { public void onSuccess(ResponseParams response) { tv.append(response.requestParams().url() + "成功putJSON\n"); // User b = response.parserObject();//解析好的模型 -// b.getUserid(); +// b.getUserName(); } @Override @@ -161,7 +161,7 @@ public void onFailure(HttpException e) { public void onSuccess(ResponseParams response) { tv.append(response.requestParams().url() + "成功patch\n"); // User b = response.parserObject();//解析好的模型 -// b.getUserid(); +// b.getUserName(); } @Override @@ -226,41 +226,55 @@ 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") - .param("userid", 10086) - .param("password", "qwe123456") - .buildAndExecute(new MyHttpCallback() { + .jsonBody(dataUser) + .buildAndExecute(new MyHttpCallback>() { @Override - public void onComplete(String dataUser) { - tv.append("MyHttpCallback.String=" + dataUser + "\n"); + public void onComplete(User dataUser) { + tv.append("MyHttpCallback.User=" + JSON.toJSONString(dataUser) + dataUser.getRows().get(0).getClass() + "\n"); } }); - User dataUser = new User(); - User dataUser2 = new User(); - dataUser.setPassword("setPassword"); - dataUser2.setUserid("setUserid"); QSHttp.postJSON("https://api.reol.top/test/json") - .jsonBody(Arrays.asList(dataUser, dataUser2)) - .buildAndExecute(new MyHttpCallback>() { + .header("string", "{\"status\":0,\"data\":3.6}") + .buildAndExecute(new MyHttpCallback() { @Override - public void onComplete(List dataUser) { - tv.append("MyHttpCallback.List=" + JSON.toJSONString(dataUser) + "\n"); + 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(Arrays.asList(dataUser, dataUser2)) + .jsonBody(users) .buildAndExecute(new QSHttpCallback>() { @Override public void onComplete(List dataUser) { - tv.append("QSHttpCallback.List=" + JSON.toJSONString(dataUser) + "\n"); + 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("string","3.6") + .header("string", "3.6") .buildAndExecute(new QSHttpCallback() { @Override public void onComplete(String dataUser) { @@ -326,7 +340,7 @@ public void onSuccess(ResponseParams response) { //获得自动解析/自定义解析的结果 User b = response.parserObject(); - b.getUserid(); + b.getUserName(); } @Override diff --git a/app/src/main/java/com/song/demo/MyHttpCallback2.java b/app/src/main/java/com/song/demo/MyHttpCallback2.java index eb51dac..7ac2b4b 100644 --- a/app/src/main/java/com/song/demo/MyHttpCallback2.java +++ b/app/src/main/java/com/song/demo/MyHttpCallback2.java @@ -7,7 +7,7 @@ /* * Created by song on 2019/5/16. * 根据自己的项目对回调进行再包装 - * todo 目前用不了 + * todo 目前用不了 QSHttpCallback无法获取到ResultModel,只能获取到M */ @Deprecated public abstract class MyHttpCallback2 extends QSHttpCallback> { diff --git a/app/src/main/java/com/song/demo/User.java b/app/src/main/java/com/song/demo/User.java index b2b5e7e..b4c3f5f 100644 --- a/app/src/main/java/com/song/demo/User.java +++ b/app/src/main/java/com/song/demo/User.java @@ -1,28 +1,32 @@ package com.song.demo; import java.io.Serializable; +import java.util.List; /** * Created by song on 2017/2/16. */ -public class User implements Serializable { - private String userid; - private String password; +public class User implements Serializable { - public String getUserid() { - return userid; + private String userName; + + private List rows; + + public String getUserName() { + return userName; } - public void setUserid(String userid) { - this.userid = userid; + public void setUserName(String userName) { + this.userName = userName; } - public String getPassword() { - return password; + + public List getRows() { + return rows; } - public void setPassword(String password) { - this.password = password; + public void setRows(List rows) { + this.rows = rows; } } diff --git a/qshttp/build.gradle b/qshttp/build.gradle index 7a3fde6..65a0728 100644 --- a/qshttp/build.gradle +++ b/qshttp/build.gradle @@ -6,8 +6,8 @@ android { minSdkVersion 9 targetSdkVersion compileSdkVersion 28 - versionCode 10 - versionName "1.4.2" + versionCode 11 + versionName "1.4.3" } buildTypes { release { 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 1b978d8..8af64b5 100644 --- a/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java +++ b/qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java @@ -6,14 +6,15 @@ import android.view.View; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; -import com.alibaba.fastjson.parser.ParserConfig; -import com.alibaba.fastjson.util.TypeUtils; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; /** * Created by song @@ -50,9 +51,8 @@ public void onSuccess(ResponseParams response) { } } - protected T parserT(Object json) throws JSONException { - return TypeUtils.cast(json, findT(), ParserConfig.getGlobalInstance()); + return parserT(JSON.toJSONString(json)); } protected T parserT(String json) throws JSONException { @@ -73,13 +73,14 @@ protected Type findT() { type = String.class; } else { ParameterizedType parameterizedType = (ParameterizedType) type; - //T=List - if (parameterizedType.getActualTypeArguments()[0] instanceof ParameterizedType) { - ParameterizedType parameterizedType1 = (ParameterizedType) parameterizedType.getActualTypeArguments()[0]; - type = parameterizedType1.getRawType(); - } else { - type = parameterizedType.getActualTypeArguments()[0]; - } + +// if (parameterizedType.getActualTypeArguments()[0] instanceof ParameterizedType) { +// ParameterizedType parameterizedType1 = (ParameterizedType) parameterizedType.getActualTypeArguments()[0]; +// type = parameterizedType1.getRawType(); //T=List +// Class clazz = (Class) parameterizedType1.getActualTypeArguments()[0]; +// } else { + type = parameterizedType.getActualTypeArguments()[0]; +// } } return type; } @@ -106,6 +107,14 @@ public boolean isDestroy() { return activity != null && activity.isFinishing(); } + public static List parserList(JSONArray jsonArray, Class _class) { + List list = new ArrayList<>(); + if (jsonArray != null) + for (int i = 0; i < jsonArray.size(); i++) { + list.add(jsonArray.getJSONObject(i).toJavaObject(_class)); + } + return list; + } public static Activity findActivity(Object o) { //获取外部类 @@ -165,7 +174,6 @@ public static Object field(Object base, String fieldName) { f.setAccessible(true); return f.get(base); } catch (Exception e) { - e.printStackTrace(); } return null; }