2012-07-10 64 views
1

我想設置我的應用程序在HttpPost 3秒後引發超時異常。我正在從ASyncTask執行此請求。出於某種原因,即使我給它一個不存在的域,它也會掛起大約一兩分鐘,然後拋出最後一個異常。我的超時例外怎麼不起作用?Android的HttpPost超時異常不工作

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; 

     } 

回答

1

不要在那裏叫完。

爲什麼當出現帶有特定錯誤消息的異常時不返回空值。

在postexecute方法中,當結果爲null時,做任何需要做的事情。

+0

我不是故意要保持完成的在那裏......我在最後一分鐘裏扔了一下,看看是否會完成異常活動。我編輯了我的帖子。 – 2012-07-10 18:13:11

+0

通過將我的任務移動到onPostExecute來實現它。謝謝! – 2012-07-10 18:38:40

+0

@你以前的任務是什麼? – Hades 2012-07-12 07:08:16