当前位置:操作系统 > 安卓/Android >>

[Android定义]HttpPost连接超时

[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.zzzyk.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
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,