所有其他httpClient执行需要等待其他失败执行才能在执行之前先失败?

时间:2017-04-19 07:43:35

标签: android httpclient

我有以下代码:

HttpHelper

public class HttpHelper {
    public HttpEntity httpGet(String url){
        HttpClient httpclient = Client.getThreadSafeClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            // Examine the response status
            Log.d(TAG, "Response status: " + response.getStatusLine().toString());
            HttpEntity entity = response.getEntity();
            return entity;
        } catch (Exception e) {
            Log.d(TAG, "error: " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}

客户端

public class Client {
    public static DefaultHttpClient getThreadSafeClient() {   
        DefaultHttpClient client = new DefaultHttpClient();
        ClientConnectionManager mgr = client.getConnectionManager();

        HttpParams httpParameters = client.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
        HttpConnectionParams.setSoTimeout(httpParameters, 10000);
        ConnManagerParams.setTimeout(httpParameters, 10000);

        client = new DefaultHttpClient(
            new ThreadSafeClientConnManager(httpParameters,
                mgr.getSchemeRegistry()), httpParameters);

        return client;
    } 
}

我使用此代码调用它:

HttpHelper httpHelper = new HttpHelper();
HttpEntity entity = httpHelper.httpGet(url);

然后我尝试将网址调用http://www.google.com:81,这是无法访问的。但是,所有调用都需要等待该调用先失败,然后执行。

任何人都可以建议如何解决这个问题?我知道它已被弃用但请先忽略它。

1 个答案:

答案 0 :(得分:0)

这不是HttpClient的错,而是AsyncTask

我的AsyncTask不允许同时执行。

以下是解决它的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
    task.execute();