Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> [Android定義]HttpPost連接超時

[Android定義]HttpPost連接超時

編輯:關於Android編程

[java]
public static String test(String URL, List<BasicNameValuePair> params) { 
        HttpPost httpPost = new HttpPost(URL); 
        String returnString = ""; 
        HttpParams httpParameters = new BasicHttpParams(); 
 
        try { 
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity( 
                    params, "utf-8"); 
            httpPost.setEntity(urlEncodedFormEntity); 
            HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); 
            HttpClient httpClient = new DefaultHttpClient(httpParameters); 
            HttpResponse httpResponse = httpClient.execute(httpPost); 
            HttpEntity httpEntity = httpResponse.getEntity(); 
            InputStream inputStream = httpEntity.getContent(); 
            BufferedInputStream bufferedInputStream = new BufferedInputStream( 
                    inputStream); 
            ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50); 
            int current = 0; 
            while ((current = bufferedInputStream.read()) != -1) { 
                byteArrayBuffer.append(current); 
            } 
            returnString = EncodingUtils.getString(byteArrayBuffer 
                    .toByteArray(), "utf-8"); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } catch (ClientProtocolException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return returnString; 
    }   www.2cto.com

public static String test(String URL, List<BasicNameValuePair> params) {
  HttpPost httpPost = new HttpPost(URL);
  String returnString = "";
  HttpParams httpParameters = new BasicHttpParams();

  try {
   UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
     params, "utf-8");
   httpPost.setEntity(urlEncodedFormEntity);
   HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
   HttpClient httpClient = new DefaultHttpClient(httpParameters);
   HttpResponse httpResponse = httpClient.execute(httpPost);
   HttpEntity httpEntity = httpResponse.getEntity();
   InputStream inputStream = httpEntity.getContent();
   BufferedInputStream bufferedInputStream = new BufferedInputStream(
     inputStream);
   ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50);
   int current = 0;
   while ((current = bufferedInputStream.read()) != -1) {
    byteArrayBuffer.append(current);
   }
   returnString = EncodingUtils.getString(byteArrayBuffer
     .toByteArray(), "utf-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return returnString;
 }

 作者:Etzmico
 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved