/* 
 * WebUtil.java v1.00 2015-11-6
 *
 * MaMiGuan.com
 * Copyright (c) 2008-2015 All Rights Reserved.
 */
package com.example.mountaintravel.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.util.Log;

/**
 * web请求工具类
 * 
 * @author 	chenph
 * @version 1.00
 * @date 	2015-11-6 11:02:44
 */
public class WebUtil {

	/**
	 * post请求工具
	 * 
	 * @param url
	 * 		请求的url
	 * @param parameterMap
	 * 		请求参数
	 * @return
	 * 		返回的报文，String类型
	 */
	public static String post(String url, Map<String, Object> parameterMap) {
		String result = null;
		HttpClient httpClient = new DefaultHttpClient();
		try {
			HttpPost httpPost = new HttpPost(url);
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			if (parameterMap != null) {
				for (Entry<String, Object> entry : parameterMap.entrySet()) {
					String name = entry.getKey();
					String value=StringUtils.toString(entry.getValue()) ;
				    nameValuePairs.add(new BasicNameValuePair(name, value));
				}
			}
			// 创建传递参数封装 实体对象,把实体对象存入到httpPost对象中
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			// 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if(httpResponse.getStatusLine().getStatusCode()==200){
				HttpEntity httpEntity = httpResponse.getEntity();
				result = EntityUtils.toString(httpEntity);
			}
		} catch (ClientProtocolException e) {
			System.out.println("error in the HTTP protocol"+e);
		} catch (IOException e) {
			System.out.println("post请求发生io异常"+e);
		} finally {
			// 6. 释放连接。无论执行方法是否成功，都必须释放连接
			httpClient.getConnectionManager().shutdown();
		}
		return result;
	}
	
	/**
	 * get请求工具
	 * 
	 * @param url
	 * 		请求的url
	 * @param parameterMap
	 * 		请求参数
	 * @return
	 * 		返回的报文，String类型
	 */
	public static String get(String url, Map<String, Object> parameterMap) {
		//Assert.hasText(url);
		String result = null;
		HttpClient httpClient = new DefaultHttpClient();
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			if (parameterMap != null) {
				for (Entry<String, Object> entry : parameterMap.entrySet()) {
					String name = entry.getKey();
				    String value=StringUtils.toString(entry.getValue()) ;
				 // 往这个集合中添加你要传递的参数
				    nameValuePairs.add(new BasicNameValuePair(name, value));
				}
			}
			HttpGet httpGet = new HttpGet(url + (StringUtils.contains(url, "?") ? "&" : "?") + EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")));
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			if(httpResponse.getStatusLine().getStatusCode()==200){
				result = EntityUtils.toString(httpEntity);
			}
		} catch (ClientProtocolException e) {
			//logger.error("error in the HTTP protocol", e);
		} catch (IOException e) {
			//logger.error("get请求发生io异常", e);
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return result;
	}
	
}
