- 定义一个接口
@RemoteApi("https://console.tim.qq.com/v4/sns")
public interface ImRemoteMethod {
@RemoteMethod("/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json")
Map get(@Param("random") int random);
@RemoteMethod(value = "/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json",
method = RequestMethod.POST, dataType = RequestDataType.APPLICATION_FORM_URLENCODED)
Map post(@RequestBody Map map, @Param("test2") String test2);
}
- 调用该方法
public class RemoteMethodInvokeTest {
@Test
public void generate() {
ImRemoteMethod imRemoteMethod = RemoteApiManager.generateInstance(ImRemoteMethod.class);
Map response = imRemoteMethod.get(111);
System.out.println(JsonUtils.format(response));
response = imRemoteMethod.post(Collections.singletonMap("test", "test2"), "hello");
System.out.println(JsonUtils.format(response));
}
}
- 输出结果
{"ActionStatus":"FAIL","ErrorCode":70032,"ErrorInfo":""}
{"ActionStatus":"FAIL","ErrorCode":70032,"ErrorInfo":""}
- 通过注解设置Header
@RemoteMethod(value = "https://console.tim.qq.com/v4/sns/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json", method = RequestMethod.POST)
@Header(name = "test", value = "test2")
Map post(@RequestBody Map map, @Param("test2") String test2);
- 通过Parameter Annotation 的形式添加请求头
@RemoteMethod(value = "https://console.tim.qq.com/v4/sns/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json", method = RequestMethod.POST)
@Header(name = "test", value = "test2")
Map post(@RequestBody Map map, @Param("test2") String test2, @Header(name = "test") int headerValue);
- 通过提供对应的Handler设置参数
public class PreHanderTest implements RemotePreHandler {
@Override
public void handler(RequestHandler requestHandler) {
requestHandler.addHeader(Long.toString(System.currentTimeMillis()), "testHeader");
}
}
@RemoteMethod(value = "https://console.tim.qq.com/v4/sns/friend_update?usersig=xxx&identifier=admin&sdkappid=88888888&random=99999999&contenttype=json", method = RequestMethod.POST)
@PreHandler(PreHanderTest.class)
Map post(@RequestBody Map map, @Param("test2") String test2);
- 通过Manager对象设置请求头
RemoteApiManager remoteApiManager = RemoteApiManager.getInstance();
remoteApiManager.addHeader("globalTestHeader", "test12324");
ImRemoteMethod imRemoteMethod = remoteApiManager.generate(ImRemoteMethod.class);
retrofit