码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient联网工具类

时间:2014-12-12 16:44:05      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:http协议   android应用   

public class HttpConnect {
	public static String getNews(String url,List<? extends NameValuePair> parameters) {
		StringBuilder sb = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpPost post=new HttpPost(url);
		
		HttpParams params = client.getParams();
		HttpConnectionParams.setConnectionTimeout(params, 5000);
		HttpConnectionParams.setSoTimeout(params, 5000);

		try {
			UrlEncodedFormEntity sendDate=new UrlEncodedFormEntity(parameters);
			post.setEntity(sendDate);
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(entity.getContent(), "UTF-8"),
							8192);
					String line = null;
					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					reader.close();
					return sb.toString();
				}
			}
			
		} catch (Exception e) {

		}

		return null;
	}
	

HttpClient联网工具类

标签:http协议   android应用   

原文地址:http://blog.csdn.net/u014600432/article/details/41894481

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!