网络连接请求

来源:互联网 发布:嫁给大山的女人知乎 编辑:程序博客网 时间:2024/05/29 18:24
private String commmonRequest(String url, ArrayList<NameValuePair> params) {
String strResult = null;
try {
HttpEntityEnclosingRequestBase httpRequest = new HttpPost(url);
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpClient client = new DefaultHttpClient();
// 请求超时
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTTIMEOUT);
// 读取超时
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
READTIMEOUT);
HttpResponse httpResponse = client.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
strResult = EntityUtils.toString(httpResponse.getEntity())
.toString().trim();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
strResult = null;
} catch (ClientProtocolException e) {
e.printStackTrace();
strResult = null;
} catch (ParseException e) {
e.printStackTrace();
strResult = null;
} catch (IOException e) {
e.printStackTrace();
strResult = null;
}
return strResult;
}
0 0
原创粉丝点击