我是 android 注释的新手,所以也许我遗漏了一些东西,但我基本上是在尝试复制这里看到的其余客户端https://github.com/excilys/androidannotations/wiki/Rest%20API
我有一个定义如下的拦截器
@EBean(scope = Scope.Singleton)
public class RestInterceptor implements ClientHttpRequestInterceptor{
final String TAG = "rest";
@Bean
AuthStore authStore;
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution)
throws IOException{
//Need to set the api key here but nothing happens code quits
// Log.d("Rest",authStore.getSessionKey());
HttpHeaders headers = request.getHeaders();
headers.set("api_key",authStore.getApiKey());
ClientHttpResponse resp = execution.execute(request, data);
HttpStatus code = resp.getStatusCode();
Log.d(TAG,resp.getBody().toString());
if(code.value() == 200){
Log.d(TAG,"success code 200");
}
else{
Log.d(TAG,"fail code" + code.toString());
}
return resp;
}
}
但是当我尝试添加额外的标头时,代码就退出了。auth store 类看起来像这样
@EBean(scope = Scope.Singleton)
public class AuthStore {
public String apiKey,sessionKey;
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getSessionKey() {
return sessionKey;
}
public void setSessionKey(String sessionKey) {
this.sessionKey = sessionKey;
}
}
休息界面
@Rest(rootUrl = "https://pathtoapi/rest/public/", converters = { GsonHttpMessageConverter.class }, interceptors = {RestInterceptor.class })
public interface ApiClient {
@Get("force_login")
ApiSession forceLogin();
// if you need to add some configuration to the Spring RestTemplate.
RestTemplate getRestTemplate();
void setRestTemplate(RestTemplate restTemplate);
}