Android開發基礎教程:定義HttpPost連接超時
- 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;
- }