Detailed code:
Http://www.cnblogs.com/tiantianbyconan/p/3540427.html
Androidinject Project I wrote an open-source project that uses annotation injection to simplify code
Https://github.com/wangjiegulu/androidInject
Today's new features are as follows:
1. Add @aiscreensize annotations, for attributes, to inject the current device's screen size (wide height)
2. Increase support for network requests, using dynamic proxy implementations: @AIGet annotations, for interface methods, to request URLs with GET, @AIPost annotations, for interface methods, to request URLs with post, @AIParam to inject request parameters
3. Add @ainetworker annotations, for attributes, to inject network request service
4. Request parameters can be passed in using the params class when a GET or POST request is added, simplifying the code
Tags: Gson
[1]. Code [Java] Code Skip to [1] ?
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778 |
// 用@AINetWorker注解注入NetWorker接口的子类代理(动态代理模式):@AINetWorkerprivate PersonWorker personWorker;// 然后启动线程,在线程中调用进行网络请求:new Thread(new Runnable() { @Override publicvoid run() { // RetMessage<Person> retMsg = personWorker.getPersonsForGet("a1", "b1", "c1");// RetMessage<Person> retMsg = personWorker.getPersonsForGet2(new Params().add("aa", "a1").add("bb", "b1").add("cc", "c1"));RetMessage<Person> retMsg = personWorker.getPersonsForPost2(newParams().add("aa","a1").add("bb","b1").add("cc","c1")); System.out.println(retMsg.getList().toString()); }}).start();// 请求的结果封装在RetMessage类中(AndroidInject框架所作的事就是执行Get或者Post请求,获得返回结果,然后json解析后封装在RetMessage中):/** * Json响应结果包装类* Created with IntelliJ IDEA.* Author: wangjie email:[email protected]* Date: 14-2-7* Time: 下午4:25*/public class RetMessage<T> { privateint resultCode; // 结果码,必须包含 privateList<T> list; // 返回的数据 privateT obj; // 返回的数据 privateInteger size; // 返回数据长度 privateString errorMessage; // 返回错误信息 publicString toJson(){ return new Gson().toJson(this); } // getter和setter方法省略...} // 接下来看下PersonWorker接口中所作的事情:/** * Created with IntelliJ IDEA.* Author: wangjie email:[email protected]* Date: 14-2-7* Time: 下午1:44*/public interfacePersonWorker { @AIGet("http://192.168.2.198:8080/HelloSpringMVC/person/findPersons?aa=#{a3}&bb=#{b3}&cc=#{c3}") publicRetMessage<Person> getPersonsForGet(@AIParam("a3")String a2,@AIParam("b3") String b2,@AIParam("c3") String c2); @AIPost("http://192.168.2.198:8080/HelloSpringMVC/person/findPersons") publicRetMessage<Person> getPersonsForPost(@AIParam("aa")String a2,@AIParam("bb") String b2,@AIParam("cc") String c2); @AIGet("http://192.168.2.198:8080/HelloSpringMVC/person/findPersons") publicRetMessage<Person> getPersonsForGet2(Params params); @AIPost("http://192.168.2.198:8080/HelloSpringMVC/person/findPersons") publicRetMessage<Person> getPersonsForPost2(Params params); } /***另:如果,你需要写一个查询User信息的网络请求,应该怎么写?*只需编写UserWorker接口,然后声明方法findUsers(),写上@AIGet或者@AIPost注解,写明url和请求参*数。然后通过@AINetWorker注解注入userWorker,然后开启线程,调用userWorker的findUsers()方法即可。*当然UserWorker也可以不使用注解获得,而是调用“NetInvoHandler.getWorker(UserWorker.class)”获得!*/ |
Androidinject Project uses dynamic agents to increase support for network requests