类似restful风格的网络请求框架?是个什么鬼,咋一听,感觉很高大上的样子,restful风格是怎么样的一种风格?我们去投简历的时候,会有公司要求android开发人员熟悉restful风格编码,可是restful是一种web服务器api的一种架构风格,它提供了一组设计原则和约束条件,跟android开发有什么关系呢?目前还没有搞懂,如果有哪位大牛知道,希望不吝赐教!那今天说的类似restful风格的网络请求框架,具体是不是我也不知道,但感觉应该是,并且本例子不会给出网络部分的实现,只是搭一个框架!
首先说一下需要用到的知识点:反射,动态代理,注解!ok,看代码!
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Get {
String value() default "";
}</span>
POST请求:
<span style="font-size:18px;">@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Post {
String value() default "";
}</span>
参数PARAMES
<span style="font-size:18px;">@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Parames {
String value() default "";
}</span>
接下来需要一个请求的接口,用来存放所以请求
<span style="font-size:18px;">public interface NetApi {
@Get("/api/reflectannotionproxy/getData")
Result getData(@Parames("type") String type, @Parames("description") String description);
@Post("/api/reflectannotionproxy/postData")
Result postData(@Parames("type") String type, @Parames("description") String description);
}</span>
网络请求之后需要返回一个数据给你,我们把它封装成一个java bean
<span style="font-size:18px;">public class Result {
private String type;
private String description;
@Override
public String toString() {
return "Result{" +
"type='" + type + '\'' +
", description='" + description + '\'' +
'}';
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}</span>
接下来我们需要一个动态代理,来处理这些请求
<span style="font-size:18px;">public enum RequestType {
GET(0),
POST(1);
RequestType(int i) {
type = i;
}
int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}</span>
<span style="font-size:18px;">public class NetUtil implements InvocationHandler {
private static final String URL = "http://192.168.11.20:8080";
private RequestType requestType;
public NetUtil() {
requestType = RequestType.GET;
}
public NetUtil(RequestType requestType) {
this.requestType = requestType;
}
@Override
public Result invoke(Object proxy, Method method, Object[] args) throws Throwable {
Result result = new Result();
String url = "";
String type = "";
String description = "";
if (method.isAnnotationPresent(Get.class)) {
Get get = method.getAnnotation(Get.class);
requestType = RequestType.GET;
url = URL + get.value();
}
if (method.isAnnotationPresent(Post.class)) {
Post post = method.getAnnotation(Post.class);
requestType = RequestType.POST;
url = URL + post.value();
}
ArrayList<String> arrayList = new ArrayList<>();
Annotation[][] annotations = method.getParameterAnnotations();
if (annotations != null && annotations.length > 0) {
for (int i = 0;i<annotations.length;i++) {
Annotation[] annotations1 = annotations[i];
Log.i("info", "annotations1========" + annotations1);
Log.i("info", "annotations1.length========" + annotations1.length);
for (Annotation annotation : annotations1) {
Log.i("info", "annotation========" + annotation);
Log.i("info", "annotation instanceof Parames========" + (annotation instanceof Parames?true:false));
if (annotation instanceof Parames) {
Parames parames = ((Parames) annotation);
arrayList.add(parames.value()+"-"+args[i]);
}
}
}
}
result.setType(arrayList.get(0));
result.setDescription(arrayList.get(1));
Log.i("info",url);
return result;
}
}</span>
基本的架子搭建好了,接下来在activity当中调用
<span style="font-size:18px;">public void onClick(View view) {
NetUtil netUtil = new NetUtil();
final NetApi netApi = (NetApi)Proxy.newProxyInstance(NetApi.class.getClassLoader(),new Class[]{NetApi.class},netUtil);
Result result = netApi.getData("get方法","这是一个get方法");
Toast.makeText(this,result.toString(),Toast.LENGTH_SHORT).show();
view.postDelayed(new Runnable() {
@Override
public void run() {
Result result = netApi.postData("post方法","现在执行的是post方法");
Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_SHORT).show();
}
},3000);
}</span>
里面的一些方法在前面几篇博客当中已经接受了,有不清楚的可以去看一下,下面会给出例子源码,欢迎各位给我指正错误!