【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;
	}

你可能感兴趣的:(android,String,url)