标签:
详细代码:标签:Gson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// 用@AINetWorker注解注入NetWorker接口的子类代理(动态代理模式):
@AINetWorker
private PersonWorker personWorker;
// 然后启动线程,在线程中调用进行网络请求:
new
Thread( new Runnable() {
@Override public
void 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( new
Params().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:tiantian.china.2@gmial.com
* Date: 14-2-7
* Time: 下午4:25
*/
public class
RetMessage<T> { private
int resultCode;
// 结果码,必须包含 private
List<T> list; // 返回的数据
private
T obj; // 返回的数据
private
Integer size; // 返回数据长度
private
String errorMessage; // 返回错误信息
public
String toJson(){
return new
Gson().toJson( this );
}
// getter和setter方法省略...
}
// 接下来看下PersonWorker接口中所作的事情:
/**
* Created with IntelliJ IDEA.
* Author: wangjie email:tiantian.china.2@gmail.com
* Date: 14-2-7
* Time: 下午1:44
*/
public interface
PersonWorker { @AIGet ( "http://192.168.2.198:8080/HelloSpringMVC/person/findPersons?aa=#{a3}&bb=#{b3}&cc=#{c3}" )
public
RetMessage<Person> getPersonsForGet( @AIParam ( "a3" )String a2,
@AIParam ( "b3" ) String b2,
@AIParam ( "c3" ) String c2);
@AIPost ( "http://192.168.2.198:8080/HelloSpringMVC/person/findPersons" )
public
RetMessage<Person> getPersonsForPost( @AIParam ( "aa" )String a2,
@AIParam ( "bb" ) String b2,
@AIParam ( "cc" ) String c2);
@AIGet ( "http://192.168.2.198:8080/HelloSpringMVC/person/findPersons" )
public
RetMessage<Person> getPersonsForGet2(Params params);
@AIPost ( "http://192.168.2.198:8080/HelloSpringMVC/person/findPersons" )
public
RetMessage<Person> getPersonsForPost2(Params params);
} /**
*另:如果,你需要写一个查询User信息的网络请求,应该怎么写?
*只需编写UserWorker接口,然后声明方法findUsers(),写上@AIGet或者@AIPost注解,写明url和请求参*数。然后通过@AINetWorker注解注入userWorker,然后开启线程,调用userWorker的findUsers()方法即可。
*当然UserWorker也可以不使用注解获得,而是调用“NetInvoHandler.getWorker(UserWorker.class)”获得!
*/ |
AndroidInject项目使用动态代理增加对网络请求的支持
标签:
原文地址:http://blog.csdn.net/u014311051/article/details/42367257