Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
YolandaCho committed Jul 18, 2019
1 parent 35d0b7a commit 87e03c3
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 53 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ QSHttp
### Gradle
```
dependencies {
implementation 'com.github.tohodog:QSHttp:1.4.2'
implementation 'com.github.tohodog:QSHttp:1.4.3'
}
```

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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<Bean>() {
@Override
Expand Down Expand Up @@ -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方式上传一个字节数组
Expand Down Expand Up @@ -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())//键值对参数
Expand Down Expand Up @@ -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)
Expand Down
56 changes: 35 additions & 21 deletions app/src/main/java/com/song/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void onCreate(Bundle savedInstanceState) {
downGET(url);
upLoad(url);

parserJson();
// parserJson();

}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<User> users = Arrays.asList(dataUser2);
dataUser.setRows(users);


QSHttp.postJSON("https://api.reol.top/test/json")
.param("userid", 10086)
.param("password", "qwe123456")
.buildAndExecute(new MyHttpCallback<String>() {
.jsonBody(dataUser)
.buildAndExecute(new MyHttpCallback<User<User>>() {
@Override
public void onComplete(String dataUser) {
tv.append("MyHttpCallback.String=" + dataUser + "\n");
public void onComplete(User<User> dataUser) {
tv.append("MyHttpCallback.User<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<List<User>>() {
.header("string", "{\"status\":0,\"data\":3.6}")
.buildAndExecute(new MyHttpCallback<Double>() {
@Override
public void onComplete(List<User> dataUser) {
tv.append("MyHttpCallback.List<User>=" + 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<List<User>>() {
@Override
public void onComplete(List<User> dataUser) {
tv.append("QSHttpCallback.List<User>=" + JSON.toJSONString(dataUser) + "\n");
tv.append("QSHttpCallback.List<User>=" + JSON.toJSONString(dataUser) + dataUser.get(0).getClass() + "\n");
}
});

QSHttp.postJSON("https://api.reol.top/test/json")
.header("row", "row")
.jsonBody(dataUser)
.buildAndExecute(new QSHttpCallback<User>() {
@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<String>() {
@Override
public void onComplete(String dataUser) {
Expand Down Expand Up @@ -326,7 +340,7 @@ public void onSuccess(ResponseParams response) {

//获得自动解析/自定义解析的结果
User b = response.parserObject();
b.getUserid();
b.getUserName();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/song/demo/MyHttpCallback2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/*
* Created by song on 2019/5/16.
* 根据自己的项目对回调进行再包装
* todo 目前用不了
* todo 目前用不了 QSHttpCallback无法获取到ResultModel<M>,只能获取到M
*/
@Deprecated
public abstract class MyHttpCallback2<M> extends QSHttpCallback<ResultModel<M>> {
Expand Down
26 changes: 15 additions & 11 deletions app/src/main/java/com/song/demo/User.java
Original file line number Diff line number Diff line change
@@ -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<M> implements Serializable {

public String getUserid() {
return userid;
private String userName;

private List<M> 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<M> getRows() {
return rows;
}

public void setPassword(String password) {
this.password = password;
public void setRows(List<M> rows) {
this.rows = rows;
}
}
4 changes: 2 additions & 2 deletions qshttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
minSdkVersion 9
targetSdkVersion
compileSdkVersion 28
versionCode 10
versionName "1.4.2"
versionCode 11
versionName "1.4.3"
}
buildTypes {
release {
Expand Down
32 changes: 20 additions & 12 deletions qshttp/src/main/java/org/song/http/framework/QSHttpCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -73,13 +73,14 @@ protected Type findT() {
type = String.class;
} else {
ParameterizedType parameterizedType = (ParameterizedType) type;
//T=List<xxx>
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<xxx>
// Class<?> clazz = (Class<?>) parameterizedType1.getActualTypeArguments()[0];
// } else {
type = parameterizedType.getActualTypeArguments()[0];
// }
}
return type;
}
Expand All @@ -106,6 +107,14 @@ public boolean isDestroy() {
return activity != null && activity.isFinishing();
}

public static <T> List<T> parserList(JSONArray jsonArray, Class<T> _class) {
List<T> 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) {
//获取外部类
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 87e03c3

Please sign in to comment.