类似restful风格的网络请求框架

类似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>
里面的一些方法在前面几篇博客当中已经接受了,有不清楚的可以去看一下,下面会给出例子源码,欢迎各位给我指正错误!






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值