1

我正在尝试将我的应用程序设置为在 HttpPost 上 3 秒后引发超时异常。我正在从 ASyncTask 执行此请求。出于某种原因,即使我给它一个不存在的域,它也会挂起大约一两分钟,然后抛出最后一个异常。为什么我的超时异常不起作用?

protected Void doInBackground(String... params) {
            HttpPost httpPost = new HttpPost("http://hgfdhgfdhgfdhfgdhgfdhgfdhfgd.com");
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
            HttpConnectionParams.setSoTimeout(httpParameters, 3000);
            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is =  httpEntity.getContent();  

            } catch(ConnectTimeoutException e){
                Log.e("Timeout Exception: ", e.toString());
            } catch(SocketTimeoutException ste){    
                Log.e("Timeout Exception: ", ste.toString());
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection "+e.toString());
            }

            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = "";
                while((line=br.readLine())!=null){
                    sb.append(line+"\n");
                }
                is.close();
                result=sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result "+e.toString());
            }
            return null;

        }
4

1 回答 1

1

不要在那里调用完成。

为什么在抛出带有特定错误消息的异常时不返回 null 。

在 postexecute 方法中,当结果为空时,做任何需要做的事情。

于 2012-07-10T18:11:38.730 回答