DefaultHttpClient导致异步任务延迟

时间:2014-12-31 08:08:59

标签: java android android-4.4-kitkat http-get

我正在开发一个应用程序,它使用 DefaultHttpClient 向服务器发送请求。我正在使用GET方法发送请求。如果端口号错误,那么我得到连接超时异常。我使用了处理程序例外&发生异常时返回值。但可悲的部分是后执行在60秒后被调用。

protected String doInBackground(String... url) 
    {

        HttpURLConnection con = null;
        HttpGet httpGet = null;
        DefaultHttpClient httpClient=null;

        try 
          { 
            httpGet = new HttpGet(url[0]);
            HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 5000;
            int sockettimeout=5000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            HttpConnectionParams.setSoTimeout(httpParameters, sockettimeout);
            httpClient = new DefaultHttpClient(httpParameters);               
             HttpResponse httpResponse = httpClient.execute(httpGet );
             int code=httpResponse.getStatusLine().getStatusCode();
}
catch (IOException e)
          {

             if(e.toString().contains("org.apache.http.conn.ConnectTimeoutException"))
             {
                 Log.i("RAE", "Connection Timeedout");
                 httpClient.getConnectionManager().closeExpiredConnections();
                 httpClient.getConnectionManager().shutdown();

                 httpGet.abort();



                     Log.i("log", "Before  Return");
                    return "done";
             }
@Override
    protected void onPostExecute(String result) {
             Log.i("log", "Async finished");


}

但是在60秒后调用帖子。我知道它为什么会发生

0 个答案:

没有答案