3

カスタムヘッダーで POST リクエストを作成したい。AA Rest API - https://github.com/excilys/androidannotations/wiki/Rest%20APIを使用してこれを行う方法に関する情報が見つかりません。

認証されたリクエストに使用される ClientHttpRequestInterceptor を使用する必要がありますか? https://github.com/excilys/androidannotations/wiki/Authenticated-Rest-Client

助けてくれてありがとう!

4

2 に答える 2

4

現在、これには未解決の問題があります:https ://github.com/excilys/androidannotations/issues/323

今のところ、これを行う唯一の方法は、カスタムClientHttpRequestInterceptorを使用することです。ここに小さな例があります:

@EBean
public class CustomHeaderInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
        request.getHeaders().add("myHeader", "value");
        return execution.execute(request, data);
    }

}

次に、次のように、それをrestTemplateにリンクする必要があります。

@EBean
public class MyService {

    @RestService
    RestClient restClient;

    @Bean
    MobileParametersInterceptor mobileParametersInterceptor;

    @AfterInject
    public void init() {
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(mobileParametersInterceptor);
        restClient.getRestTemplate().setInterceptors(interceptors);
    }

}
于 2012-09-28T08:50:50.247 に答える
1

実際、カスタム ヘッダーには ClientHttpRequestInterceptor を使用する必要があります。現在、私が知っている唯一の方法です。

RestTemplate の詳細については、Spring-Androidの公式ドキュメントを参照してください。

于 2012-09-28T09:01:55.403 に答える