之前的GT库内封装了 OkHttp、OKGO 的两种网络请求,还封装了原始的网络请求,但在GT库版本 V1.3.1的时候就去掉了这三种封装并加入了新的网络请求框架,目前最新版GT库的网络请求有两种 网络请求框架,一种是 简易网络请求 另一种 接口网络请求 ,在项目的复杂度上可以选择相应的网络请求框架
在使用GT库里封装的架构当然需要先依赖好GT库:
详细依赖教程请参看
我们先来看看第一种,也是最容易上手的 简易网络请求
Get 请求
String urlApi = "https://apis.map.qq.com/ws/geocoder/v1/?location=" +
"22.5948,114.3069163" +
"&key=J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT&get_poi=1";
new GT.HttpUtil().getRequest(urlApi, new GT.HttpUtil.OnLoadData() {
@Override
public void onSuccess(String response, Object o) {
super.onSuccess(response, o);
GT.logt("请求成功:" + response);
}
@Override
public void onError(String response, Object o) {
super.onError(response, o);
GT.logt("请求失败:" + response);
}
});
这种方式是最简易且快捷的网络请求,当然可以在这个简易的get请求上加上一个实体类,那么就可以直接将请求下来的数据,直接转为实体类进行操作了,这样可以免掉了 String 转 Json 的步骤:
String urlApi = "https://apis.map.qq.com/ws/geocoder/v1/?" +
"location=22.5948,114.3069163&" +
"key=J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT&" +
"get_poi=1";
new GT.HttpUtil().getRequest(urlApi, new GT.HttpUtil.OnLoadData<JsonRootBean>() {
@Override
public void onSuccess(String response, JsonRootBean jsonRootBean) {
super.onSuccess(response, jsonRootBean);
GT.logt("请求成功:" + jsonRootBean);
}
@Override
public void onError(String response, JsonRootBean jsonRootBean) {
super.onError(response, jsonRootBean);
GT.logt("请求失败:" + response);
}
});
实体类:
public class JsonRootBean {
private int status;
private String message;
private String request_id;
private Object result;
public void setStatus(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setRequest_id(String request_id) {
this.request_id = request_id;
}
public String getRequest_id() {
return request_id;
}
public void setResult(Object result) {
this.result = result;
}
public Object getResult() {
return result;
}
@Override
public String toString() {
return "JsonRootBean{" +
"status=" + status +
", message='" + message + '\'' +
", request_id='" + request_id + '\'' +
", result=" + result +
'}';
}
}
当然,get请求也支持 同步或异步的操作,但默认就是异步操作,如果想进行同步请求的话也是非常简单,在参数的最后面,加上一个 true或false即可,如这样:
String url = "https://apis.map.qq.com/ws/geocoder/v1/";
Map<String,Object> map = new HashMap<>();
map.put("location","22.5948,114.3069163");
map.put("key","J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT");
map.put("get_poi","1");
new GT.HttpUtil().getRequest(url,map, new GT.HttpUtil.OnLoadData<JsonRootBean>() {
@Override
public void onSuccess(String response, JsonRootBean jsonRootBean) {
super.onSuccess(response, jsonRootBean);
GT.logt("请求成功:" + jsonRootBean);
}
@Override
public void onError(String response, JsonRootBean jsonRootBean) {
super.onError(response, jsonRootBean);
GT.logt("请求失败:" + response);
}
},true);//是否同步:true (这个参数可填,可不填)
上面就是get 请求的基本操作了,接下来我们来看看 简易的 post 操作
POST 请求
String url = "https://apis.map.qq.com/ws/geocoder/v1/";
Map<String, Object> mapHead = new HashMap<>();
mapHead.put("","");// 请求头
Map<String, Object> map = new HashMap<>();
map.put("location", "22.5948,114.3069163");
map.put("key", "J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT");
map.put("get_poi", "1");
new GT.HttpUtil().postRequest(url, map, mapHead, new GT.HttpUtil.OnLoadData<JsonRootBean>() {
@Override
public void onSuccess(String response, JsonRootBean jsonRootBean) {
super.onSuccess(response, jsonRootBean);
GT.logt("请求成功:" + jsonRootBean);
}
@Override
public void onError(String response, JsonRootBean jsonRootBean) {
super.onError(response, jsonRootBean);
GT.logt("请求失败:" + response);
}
}, false);//是否同步:true (这个参数可填可不填,根据实际情况选择)
如果不需要请求头的,可以直接去掉请求头即可:
String url = "https://apis.map.qq.com/ws/geocoder/v1/";
Map<String, Object> map = new HashMap<>();
map.put("location", "22.5948,114.3069163");
map.put("key", "J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT");
map.put("get_poi", "1");
new GT.HttpUtil().postRequest(url, map, new GT.HttpUtil.OnLoadData<JsonRootBean>() {
@Override
public void onSuccess(String response, JsonRootBean jsonRootBean) {
super.onSuccess(response, jsonRootBean);
GT.logt("请求成功:" + jsonRootBean);
}
@Override
public void onError(String response, JsonRootBean jsonRootBean) {
super.onError(response, jsonRootBean);
GT.logt("请求失败:" + response);
}
});//是否同步:true (这个参数可填可不填,根据实际情况选择)
接下来,我们来接受 简易请求框架 里一个 get post 灵活请求的一个API 接口:
String url = "https://apis.map.qq.com/ws/geocoder/v1/";
Map<String, Object> mapHead = new HashMap<>();
mapHead.put("","");// 请求头
Map<String, Object> map = new HashMap<>();
map.put("location", "22.5948,114.3069163");
map.put("key", "J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT");
map.put("get_poi", "1");
new GT.HttpUtil().httpRequest(url, map, mapHead,new GT.HttpUtil.OnLoadData<JsonRootBean>() {
@Override
public void onSuccess(String response, JsonRootBean jsonRootBean) {
super.onSuccess(response, jsonRootBean);
GT.logt("请求成功:" + jsonRootBean);
}
@Override
public void onError(String response, JsonRootBean jsonRootBean) {
super.onError(response, jsonRootBean);
GT.logt("请求失败:" + response);
}
}, "POST");//切换请求类型:POST 或 GET (目前仅支持这两种切换)
这种方式 post 与 get 需要特殊设置或灵活切换的时候,该请求默认是异步请求,请求头可填可不填
最后再附上一个设置请求类型的
GT.HttpUtil httpUtil = new GT.HttpUtil();
httpUtil.setTextType();//设置请求类型
下载文件可这么写:
String appUrl = "http://qzonestyle.gtimg.cn/qzone/vas/opensns/res/doc/AppManage.apk";
String filePath = GT.FileUtils.getAppDirectory(this) + "/gsls.apk";
GT.HttpUtil.downloadFile(appUrl, filePath, new GT.HttpUtil.OnLoadData() {
@Override
public void onDownloadStart(File file) {
super.onDownloadStart(file);
}
@Override
public void onDownloading(int progress) {
super.onDownloading(progress);
}
@Override
public void onDownloadSuccess(File file) {
super.onDownloadSuccess(file);
}
@Override
public void onDownloadFailed(Exception e) {
super.onDownloadFailed(e);
}
});
上面就是简单网络请求,接下来我们来看看另一种,接口请求,其实接口请求使用的方式类似于 Retrofit 框架,但也支持简易网络请求 GT.HttpCall
我们先来看看 HttpCall 的简易请求方式:
简易版 POST 同步—异步请求
//异步请求
Map<String, Object> map = new HashMap<>();
map.put("location", "22.5948,114.3069163");
map.put("key", "J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT");
map.put("get_poi", "1");
new GT.HttpCall.Call()
.setUrl("https://apis.map.qq.com/ws/geocoder/v1")
.setType("POST")
.addParamMap(map)
// .addHeader("","")//添加请求头
.newCall(new GT.HttpCall.Callback<JsonRootBean>() {
@Override
public void onSuccess(JsonRootBean jsonBean, GT.HttpCall.Call<JsonRootBean> call) {
super.onSuccess(jsonBean, call);
GT.logt("jsonBean:" + jsonBean);
}
@Override
public void onError(GT.HttpCall.Call<JsonRootBean> call, String e) {
super.onError(call, e);
GT.logt("请求失败:" + e);
}
});
//同步请求(需要自行加入线程)
Map<String, Object> map = new HashMap<>();
map.put("location", "22.5948,114.3069163");
map.put("key", "J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT");
map.put("get_poi", "1");
String data = new GT.HttpCall.Call()
.setUrl("https://apis.map.qq.com/ws/geocoder/v1")
.setType("POST")
.addParamMap(map)
.newCall().getData();
GT.logt("同步请求:" + data);
简易版 GET 同步—异步请求
//异步get请求
new GT.HttpCall.Call()
.setUrl("http://********")
.setType("GET")
.newCall(new GT.HttpCall.Callback<JsonRootBean>() {
@Override
public void onSuccess(JsonRootBean jsonBean, GT.HttpCall.Call<JsonRootBean> call) {
super.onSuccess(jsonBean, call);
GT.logt("jsonBean:" + call.getData());
}
@Override
public void onError(GT.HttpCall.Call<JsonRootBean> call, String e) {
super.onError(call, e);
GT.logt("请求失败:" + e);
}
});
//同步get请求(需要自行加入线程)
String data = new GT.HttpCall.Call()
.setUrl("http://********")
.setType("GET")
.newCall().getData();
GT.logt("data:" + data);
现在我们再看看 HttpCall 的接口请求方式:
接口
@GT.HttpCall.URL("https://apis.map.qq.com/")
public interface HttpApi {
@GT.HttpCall.POST("ws/geocoder/v1")
GT.HttpCall.Call<JsonRootBean> getLocation(
@GT.HttpCall.Query("location") String location,
@GT.HttpCall.Query("key") String key,
@GT.HttpCall.Query("get_poi") String get_poi
);
@GT.HttpCall.GET("ws/geocoder/v1")
GT.HttpCall.Call<JsonBean> getCode(@GT.HttpCall.Query("key") String key);
}
请求方法:
HttpApi httpApi = GT.HttpCall.create(HttpApi.class);
//接口1 post 请求
httpApi.getLocation("22.5948,114.3069163", "J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT", "1")
.newCall(new GT.HttpCall.Callback<JsonRootBean>() {
@Override
public void onSuccess(JsonRootBean jsonRootBean, GT.HttpCall.Call<JsonRootBean> call) {
super.onSuccess(jsonRootBean, call);
GT.logt("请求成功:" + call.getData());
}
@Override
public void onError(GT.HttpCall.Call<JsonRootBean> call, String e) {
super.onError(call, e);
GT.logt("请求失败:" + call.getData());
}
});
//接口2 get 请求
httpApi.getCode("J6HBZ-N3K33-D2B3V-YH7I4-37AVE-NJFMT")
.newCall(new GT.HttpCall.Callback<JsonBean>() {
@Override
public void onSuccess(JsonBean jsonRootBean, GT.HttpCall.Call<JsonBean> call) {
super.onSuccess(jsonRootBean, call);
GT.logt("请求成功:" + call.getData());
}
@Override
public void onError(GT.HttpCall.Call<JsonBean> call, String e) {
super.onError(call, e);
GT.logt("请求失败:" + call.getData());
}
});
请求成功后返回的实体类:
public class JsonRootBean {
private int status;
private String message;
private String request_id;
private Object result;
public void setStatus(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setRequest_id(String request_id) {
this.request_id = request_id;
}
public String getRequest_id() {
return request_id;
}
public void setResult(Object result) {
this.result = result;
}
public Object getResult() {
return result;
}
@Override
public String toString() {
return "JsonRootBean{" +
"status=" + status +
", message='" + message + '\'' +
", request_id='" + request_id + '\'' +
", result=" + result +
'}';
}
}
public class JsonBean {
private int code;
private String msg;
private String data;
private String errorCode;
public void setCode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return data;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorCode() {
return errorCode;
}
@Override
public String toString() {
return "JsonBean{" +
"code=" + code +
", msg='" + msg + '\'' +
", data='" + data + '\'' +
", errorCode='" + errorCode + '\'' +
'}';
}
}
看了上面的操作是不是感觉非常像 Retrofit 框架,没错,就是按照 Retrofit 框架写的习惯来的,但也加入了新的元素,如可以类似于 OkHttp 那样简易请求,也可以像 Retrofit 框架一样接口请求,还加入了 DataBinding 元素,有没有发现写的接口里有很多 @GT.HttpCall.Query("") 注解,
如果不写注解, Retrofit 框架 肯定无法请求成功,但GT库里的 GT.HttpCall 是支持的,但需要加入 gt-DataBinding.jar 包
注册 gt-DataBinding 功能
将红框里的代码直接添加注册就可以了
提供复制粘贴,当前版本为v1.4.2.2,欢迎使用最新版本
//使用 gt-DataBinding 才需要添加以下注册,否则可以不添加
annotationProcessor 'com.github.1079374315:GSLS_Tool:v1.4.2.2'//注册 gt-DataBinding 功能
接下来就只需要 小小改一下接口类 就可以不用在写麻烦的 @GT.HttpCall.Query("") 注解了
import com.gsls.gt_databinding.annotation.GT_HttpCallBuild;
@GT_HttpCallBuild
@GT.HttpCall.URL("https://apis.map.qq.com/")
public interface HttpApi {
@GT.HttpCall.POST("ws/geocoder/v1")
GT.HttpCall.Call<JsonRootBean> getLocation(String location,String key,String get_poi);
@GT.HttpCall.GET("ws/geocoder/v1")
GT.HttpCall.Call<JsonBean> getCode(String key);
}
下载文件可以这样写:
简易版的:
String appUrl = "http://qzonestyle.gtimg.cn/qzone/vas/opensns/res/doc/AppManage.apk";
String filePath = GT.FileUtils.getAppDirectory(this) + "/gsls.apk";
new GT.HttpCall.Call()
.setUrl(appUrl)
.addFilePath(appUrl)
.addFile(new File(filePath))
.setFileState(true)
.newCall(new GT.HttpCall.Callback() {
@Override
public void onStart(GT.HttpCall.Call call) {
super.onStart(call);
GT.logt("开始下载");
}
@Override
public void onDownloading(File file, int progress, int fileProgress, int fileProgressMax, String fileProgressValue, String fileProgressMaxValue, String fileSize) {
super.onDownloading(file, progress, fileProgress, fileProgressMax, fileProgressValue, fileProgressMaxValue, fileSize);
GT.logt("下载中...进度条:" + progress + " 文件进度:" + fileProgressValue + "/" + fileProgressMaxValue);
}
@Override
public void onDownloadSuccess(File file) {
super.onDownloadSuccess(file);
GT.logt("下载完成");
}
@Override
public void onEnd(GT.HttpCall.Call call) {
super.onEnd(call);
GT.logt("结束下载");
}
});
接口版的:
@GT.HttpCall.URL("http://qzonestyle.gtimg.cn/qzone/vas/opensns/")
public interface HttpApi{
@GT.HttpCall.GET("res/doc/AppManage.apk")
GT.HttpCall.Call<File> downloadFile(@GT.HttpCall.FileDownload("filePath") String filePath);
}
String filePath = GT.FileUtils.getAppDirectory(this) + "/gsls.apk";
//接口1 post 请求
GT.HttpCall.create(HttpApi.class).downloadFile(filePath)
.newCall(new GT.HttpCall.Callback<File>() {
@Override
public void onStart(GT.HttpCall.Call<File> call) {
super.onStart(call);
GT.logt("开始下载");
}
@Override
public void onDownloading(File file, int progress, int fileProgress, int fileProgressMax, String fileProgressValue, String fileProgressMaxValue, String fileSize) {
super.onDownloading(file, progress, fileProgress, fileProgressMax, fileProgressValue, fileProgressMaxValue, fileSize);
GT.logt("下载中...进度条:" + progress + " 文件进度:" + fileProgressValue + "/" + fileProgressMaxValue);
}
@Override
public void onDownloadSuccess(File file) {
super.onDownloadSuccess(file);
GT.logt("下载完成");
}
@Override
public void onEnd(GT.HttpCall.Call<File> call) {
super.onEnd(call);
GT.logt("结束下载");
}
});
其余的运行使用代码都一样。同步异步可参考上面简易版的 HttpCall 来使用,目前 GT库 中网络请求框架 的核心使用就是这些了后期可能会不定期更新,感谢各位伙伴的关注。
点个关注点个赞呗(〃'▽'〃),关注博主最新发布库:GitHub - 1079374315/GT