アプリの循環依存に問題があるようです。私はすべてのクラスをテストしましたが、それらはうまく機能しますが、アプリの実行時にそれらをインスタンス化することはできません。
問題の根本はこのクラスにあります
public class HttpClientProvider implements Provider<HttpClient> {
@Inject
private UserAgent userAgent;
@Inject
private HttpResponseInterceptor interceptor;
private DefaultHttpClient httpClient = new DefaultHttpClient();
@Override
public HttpClient get() {
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent.getUserAgent());
httpClient.addResponseInterceptor(interceptor);
return httpClient;
}
}
このクラスはhttpClientを提供し、インターセプターを追加します。インターセプターは、このプロバイダーがこのクラスのhttpclientをプロバイダーするために使用されているように見えます
public class ServerCommunicator implements ServerCommunicator {
public static final String URL = "http://something.com/";
private HttpClient httpClient;
private HttpContext httpContext;
@Inject
public ServerCommunicator(HttpClient httpClient, HttpContext httpContext) {
this.httpClient = httpClient;
this.httpContext = httpContext;
}
@Override
public HttpResponse post(String URN, String entity) throws IOException {
HttpPost httpPost = new HttpPost(URL + URN);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(new StringEntity(entity));
return httpClient.execute(httpPost, httpContext);
}
@Override
public HttpResponse get(String URN) throws IOException {
return httpClient.execute(new HttpGet(URL + URN), httpContext);
}
@Override
public HttpResponse delete(String URN) throws IOException {
return httpClient.execute(new HttpDelete(URL + URN), httpContext);
}
}
問題が発生するのは、ユーザーが認証されていない場合、httpResponseInterceptorもServerCommunicatorクラスを使用してサーバーに要求を送信するためです。
これを解決する方法は?