Android开发基础教程:定义HttpPost连接超时


Android开发基础教程:定义HttpPost连接超时

  1. public static String test(String URL, List<BasicNameValuePair> params) {  
  2.         HttpPost httpPost = new HttpPost(URL);  
  3.         String returnString = "";  
  4.         HttpParams httpParameters = new BasicHttpParams();  
  5.   
  6.         try {  
  7.             UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(  
  8.                     params, "utf-8");  
  9.             httpPost.setEntity(urlEncodedFormEntity);  
  10.             HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);  
  11.             HttpClient httpClient = new DefaultHttpClient(httpParameters);  
  12.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  13.             HttpEntity httpEntity = httpResponse.getEntity();  
  14.             InputStream inputStream = httpEntity.getContent();  
  15.             BufferedInputStream bufferedInputStream = new BufferedInputStream(  
  16.                     inputStream);  
  17.             ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50);  
  18.             int current = 0;  
  19.             while ((current = bufferedInputStream.read()) != -1) {  
  20.                 byteArrayBuffer.append(current);  
  21.             }  
  22.             returnString = EncodingUtils.getString(byteArrayBuffer  
  23.                     .toByteArray(), "utf-8");  
  24.         } catch (UnsupportedEncodingException e) {  
  25.             e.printStackTrace();  
  26.         } catch (ClientProtocolException e) {  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         return returnString;  
  32.     }  

相关内容