java - Android HttpClient 挂起并发请求,直到我向另一个主机执行请求

标签 java android httpclient

我在使用 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;
    }
}

最佳答案

这很可能是由于您构建所使用的 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);

关于java - Android HttpClient 挂起并发请求,直到我向另一个主机执行请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10782553/

相关文章:

java - 有没有办法检查特定的 jdk(Oracle 或 OpenJDK)是否支持 Java Flight Recorder?

java - 马文 : How to deploy with deploy-file and custom wagon

c# - HttpClient.PostAsync 使用退出代码 0 淘汰应用程序

C# .NET Core 3.1 无法建立 SSL 连接

java - 在 netbeans 中的 JTable 中填充数据

java - 获取 MQJE001 后出现重复消息 : An MQException occurred: Completion Code '2' , 原因 '2018'

Android 使用自定义图 block 更改谷歌地图图 block

java - 使用查看器页面在两个 fragment 之间切换 - 但是切换到 fragment 后它消失

javascript - 我的 Android 平板电脑 4.4 版本无法自动播放视频

java - HttpClient:我可以通过多个线程为不同的域安全地重用 HttpContext 吗?