与服务器端链接的工具类

public class HttpUtil {

	// private static HttpClient client = new DefaultHttpClient();
	private static final String CHARSET = HTTP.UTF_8;
	private static HttpClient client;

	private static synchronized HttpClient getHttpClient() {
		if (null == client) {
			HttpParams params = new BasicHttpParams();
			// 设置一些基本参数
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, CHARSET);
			HttpProtocolParams.setUseExpectContinue(params, true);
			HttpProtocolParams
					.setUserAgent(
							params,
							"Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
									+ "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");

			// 超时设置/* 从连接池中取连接的超时时间 */
			ConnManagerParams.setTimeout(params, 20000);
			// /* 连接超时 */
			HttpConnectionParams.setConnectionTimeout(params, 20000);
			// /* 请求超时 */
			HttpConnectionParams.setSoTimeout(params, 20000); //
			// 设置我们的HttpClient支持HTTP和HTTPS两种模式
			SchemeRegistry schReg = new SchemeRegistry();
			schReg.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			schReg.register(new Scheme("https",
					org.apache.http.conn.ssl.SSLSocketFactory
							.getSocketFactory(), 443)); //
			// 使用线程安全的连接管理来创建HttpClient
			ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
					params, schReg);
			client = new DefaultHttpClient(conMgr, params);
		}
		return client;
	}

	/**
	 * get方式与服务器端交互
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String getRequest(String url) throws Exception {
		HttpGet get = new HttpGet(url);
		 getHttpClient();
		HttpResponse response = client.execute(get);

		if (response.getStatusLine().getStatusCode() == 200) {
			String result = EntityUtils.toString(response.getEntity());
			return result;
		}
		return null;
	}

	/**
	 * post方式与服务器端交互数据
	 * 
	 * @param url
	 * @param params
	 * @return
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static String postRequest(String url, Map<String, Object> rawParams)
			throws Exception {
		HttpPost post = new HttpPost(url);
		 getHttpClient();
		List<NameValuePair> params = new ArrayList<NameValuePair>();

		// 封装请求参数
		for (String key : rawParams.keySet()) {
			params.add(new BasicNameValuePair(key, rawParams.get(key)
					.toString()));
		}
		// 设置请求参数
		post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
		HttpResponse response = client.execute(post);
		if (response.getStatusLine().getStatusCode() == 200) {
			// 返回结果字符串
			String result = EntityUtils.toString(response.getEntity());
			return result;
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		String json = HttpUtil
				.getRequest("http://api.map.baidu.com/geocoder?address=上海市浦东新区陆家嘴&output=json&key=ab6bbb8dffddae6765c6bfab8fe49abd&city=上海");
		System.out.println(json);
		JSONObject o=JSONObject.fromObject(json);
		JSONObject result=o.optJSONObject("result");
		JSONObject location=result.optJSONObject("location");
		System.out.println(location.getString("lng"));
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值