Android HttpClient вешает одновременные запросы, пока я не выполню запрос к другому хосту

1

У меня проблема с использованием DefaultHttpClient.

Если я выполняю несколько запросов одновременно на один и тот же сервер (> 2), он будет их повесить (ответы не будут получены). Все последующие запросы тоже будут висеть.

Вот код.

public class ConnectionService {

    // Should be called once with application context
    public static void initWithContext(Context ctx) {
        // Creating custom multithread connection manager to handle multiple simultaneous requests
        HttpParams params = new BasicHttpParams();
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        ConnManagerParams.setMaxTotalConnections(params, 200);
        ConnPerRoute cpr = new ConnPerRoute() {
            @Override
            public int getMaxForRoute(HttpRoute httpRoute) {
                return 50;
            }
        };
        ConnManagerParams.setMaxConnectionsPerRoute(params, cpr);

        httpClient = new DefaultHttpClient(cm, params);
        httpClient.setCookieStore(new PersistentCookieStore(ctx));
    }

    static private DefaultHttpClient httpClient = null;

    private JsonExecutorInterface requestExecutorForRelativePathAndParams(String path, WebParams params) throws UnsupportedEncodingException {
        HttpPost postRequest = new HttpPost(rootUrl.toString() + path);

        if(params != null) {
            postRequest.setEntity(params.getFormEntity());
        }

        JsonExecutorProxy executor = new JsonExecutorProxy();
        executor.setRequest(postRequest);
        executor.setErrorsHandlerDelegate(this);

        return executor;
    }
}
Теги:
httpclient

1 ответ

0
Лучший ответ

Скорее всего, это связано с тем, как вы ThreadSafeClientConnManager вы используете. Если вы слегка измените порядок, вы должны получить лучший результат.

    HttpParams params = new BasicHttpParams();
    // The params are read in the ctor of the pool constructed by
    // ThreadSafeClientConnManager, and need to be set before constructing it.
    ConnManagerParams.setMaxTotalConnections(params, 200);
    ConnPerRoute cpr = new ConnPerRoute() {
        @Override
        public int getMaxForRoute(HttpRoute httpRoute) {
            return 50;
        }
    };
    ConnManagerParams.setMaxConnectionsPerRoute(params, cpr);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
  • 0
    О, спасибо, я забыл, что ConnectionManager также использует эти параметры.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню