English 中文(简体)
和机器人 HttpClient 挂挂同步请求, 直到我执行请求到另一个主机
原标题:Android HttpClient hangs simultaneous requests until I execute request to another host

我使用默认 HttpClient 有问题。

如果我同时执行一些请求到同一个服务器( & gt; 2), 它将会挂上( 将没有收到回复) 。 所有接下来的请求也将挂上 。

Here is the code.

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;
    }
}
最佳回答

这很可能是因为您正在如何构建您正在使用的 < code> 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);
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签