通过注解的方式来实现android中的网络请求思路
需求
- 项目中需要更改网络请求框架的时候只需要添加libs不用改其他代码
- 网络中的请求方式以及参数都通过接口和注解的方式提供
- 接口不需要实现类,类似mybatis
看看简单的demo
//定义网络接口的interface + 注解
public interface EchoService {
@Get(path = "echo.php")
void getUserName(
@Param(value = "name") String name,
@Param(value = "number") String number,
HttpCallBack callBack
);
@Post(path = "echo.php")
void postUserName(
@Param(value = "name") String name,
@Param(value = "number") String numbser,
HttpCallBack callBack
);
@Get(path = "echo.php")
void getUserAge(
@Param(value = "age") int age,
HttpCallBack callBack
);
}
使用的时候仅仅只需
EchoService echoService = XHttpClient.getServiceByClass(EchoService.class);
echoService.getUserAge(100, new HttpCallBack() {
@Override
public void success(String content) {
contentTextView.setText(content);
}
@Override
public void error(int code, String message) {
}
});
需要解决的问题
- 注解的获取,可以通过class类对象获取
- 获取到注解后通过接口返回一个实例,这个可以用动态代理实现
- 发起网络请求,由于需要在各个框架之间切换,可以用接口来实现
获取注解
Annotation[] methodAnnotations = method.getAnnotations();
boolean isGet = true;
String childPath = null;
String[] ks = null;
String[] vs = null;
for(Annotation a : methodAnnotations){
if(a instanceof Get){
Get aGet = (Get) a;
childPath = aGet.path();
isGet = true;
}
if(a instanceof Post){
Post aPost = (Post) a;
childPath = aPost.path();
isGet = false;
}
if(a instanceof Header){
Header aHeader = (Header) a;
String[] keys = aHeader.key();
String[] values = aHeader.value();
if(keys != null && values != null && keys.length == values.length && keys.length > 0){
ks = keys;
vs = values;
}
}
}//end for
动态代理
InvocationHandler
网络请求的接口
public interface HttpClient {
void executeRequest(HttpEntry httpEntry, HttpCallBack callBack);
}
不同的网络框架使用不同的实现
public class HttpClientForXUtils implements HttpClient {
@Override
public void executeRequest(HttpEntry httpEntry, HttpCallBack callBack) {
RequestParams requestParams = new RequestParams(httpEntry.getBasePath() + httpEntry.getChildPath());
requestParams.setMethod(httpEntry.getMethod() == HttpEntry.METHOD_GET ? HttpMethod.GET : HttpMethod.POST);
if(httpEntry.getParamsList() != null){
for(int i=0;i < httpEntry.getParamsList().size();i++){
ParamEntry entry = httpEntry.getParamsList().get(i);
if(entry.getValue() instanceof File){
requestParams.addBodyParameter(entry.getKey(),(File) entry.getValue());
}else{
requestParams.addBodyParameter(entry.getKey(),entry.getValue().toString());
}
}
}
if(httpEntry.getMethod() == HttpEntry.METHOD_GET){
get(requestParams,callBack);
}
if(httpEntry.getMethod() == HttpEntry.METHOD_POST){
post(requestParams,callBack);
}
}//end method
private void post(RequestParams requestParams, final HttpCallBack httpCallBack){
x.http().get(requestParams, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
if(httpCallBack != null){
httpCallBack.success(result);
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
if(httpCallBack != null){
httpCallBack.error(404,ex.getMessage());
}
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
private void get(RequestParams requestParams, final HttpCallBack httpCallBack){
x.http().get(requestParams, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
httpCallBack.success(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
httpCallBack.error(404,ex.getMessage());
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}
gihub地址
待续...