// 用@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:[email protected]
* 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:[email protected]
* 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)”获得!
*/
|