歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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.     }  
Copyright © Linux教程網 All Rights Reserved