我在Android应用中使用"com.loopj.android:android-async-http:1.4.5"。设置是每个API端点有一个以下HttpDataSource类的实例(例如:http://myhost.com/some/service/ {pathParam});因此有多个AsyncHttpClient实例(反过来又是Apache HttpClient);这可能会导致我的问题。
import java.io.IOException;
import java.net.URI;
import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
public class HttpDataSource {
private final AsyncHttpClient asyncHttpClient;
public HttpDataSource() {
asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.setUserAgent(UserAgentFactory.createUserAgent());
asyncHttpClient.setEnableRedirects(true);
addRedirectListener();
}
protected void get(final String url, final RequestParams requestParams, final HttpResponseHandler responseHandler) {
// Method not important; constructs headers for conditional GETs
final Header[] headers = checkForMetaData(url);
asyncHttpClient.get(null, url, headers, requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(final int statusCode, final Header[] headers, final byte[] responseBody) {
// we do some work then pass on to the responseHandler
responseHandler.onSuccess(statusCode, headers, responseBody);
}
@Override
public void onFailure(final int statusCode, final Header[] headers, final byte[] responseBody,
final Throwable error) {
// we do some work then pass on to the responseHandler
responseHandler.onFailure(statusCode, headers, responseBody, error);
}
});
}
private void addRedirectListener() {
getHttpClient().addResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(final HttpResponse httpResponse, final HttpContext httpContext)
throws HttpException, IOException {
if (httpResponse.getStatusLine().getStatusCode() == 302) {
final URI reqUri = ((HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST)).getURI();
final HttpHost currentHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
final String url = (reqUri.isAbsolute()) ? reqUri.toString() : (currentHost.toURI() + reqUri.getPath());
Log.e("HttpDataSource", "Got 302 for '" + url + "'");
}
}
});
}
private DefaultHttpClient getHttpClient() {
return ((DefaultHttpClient) asyncHttpClient.getHttpClient());
}
}
当我对不同的URL进行并发GET(例如:http://myhost.com/some/service/bar,http://myhost.com/some/service/foo)时,重定向侦听器会触发两次,但是使用相同的HttpContext对象,我认为这是错误的。日志是:
HttpDataSource﹕ Got 302 for 'http://myhost.com/some/service/foo'
HttpDataSource﹕ Got 302 for 'http://myhost.com/some/service/foo'
我怀疑问题出在Apache HttpClient上,而不是Loop4J的AsyncHttpClient,因为AsyncHttpClient有一个HttpClient实例,你希望它是"实例安全"。
如何在触发并发请求时返回302的URL获取正确的HttpContext对象?
答案 0 :(得分:-1)
client.setEnableRedirects(true);