package com.example.newdemo.net;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.newdemo.util.MyLog;

import android.content.Context;

public class HttpUtil {
	
	public void getLoginResponseInfo(Context context, String phone, String pswd) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(NetRequestAddress.REQUEST_ADDR_LOGIN);
		//组装数据放到HttpEntity中发送到服务器
		ArrayList<BasicNameValuePair> dataList = new ArrayList<BasicNameValuePair>();
		dataList.add(new BasicNameValuePair("phone", phone));
		dataList.add(new BasicNameValuePair("password", pswd));
		HttpEntity httpEntity = null;
		try {
			httpEntity = new UrlEncodedFormEntity(dataList, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		httpPost.setEntity(httpEntity);
		//向服务器发送POST请求并获取服务器返回的结果
		HttpResponse httpResponse = null;
		//获取响应的结果信息 
		String result = "";
		try {
			httpResponse = httpClient.execute(httpPost);
			result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
			MyLog.d("KungFuLife", "登录result ＝ " + result);
			if (result != null && !result.trim().toString().equals("")) {
				parseResultData(result, pswd);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//上传数据用的类
	public static void postInfoToServer(Context context, File mFileName, File actionUrl, String info) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(NetRequestAddress.REQUEST_ADDR_LOGIN);
		//组装数据放到HttpEntity中发送到服务器
		ArrayList<BasicNameValuePair> dataList = new ArrayList<BasicNameValuePair>();
//		dataList.add(new BasicNameValuePair("mFileName", mFileName));
//		dataList.add(new BasicNameValuePair("actionUrl", actionUrl));
		HttpEntity httpEntity = null;
		try {
			httpEntity = new UrlEncodedFormEntity(dataList, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		httpPost.setEntity(httpEntity);
		//向服务器发送POST请求并获取服务器返回的结果
		HttpResponse httpResponse = null;
		//获取响应的结果信息 
		String result = "";
		try {
			httpResponse = httpClient.execute(httpPost);
			result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
			MyLog.d("KungFuLife", "登录result ＝ " + result);
			if (result != null && !result.trim().toString().equals("")) {
//				parseResultData(result, actionUrl);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//Json解析返回的数据
	private static void parseResultData(String result, String actionUrl) {
		try {
			JSONObject jsonObject = new JSONObject(result);
			String status = jsonObject.getString("status");
			String info = "";
			if(jsonObject.has("info")){
				info = jsonObject.getString("info");
			}
			if(status != null && status.equals("success")){
//				MyToast.showToast(LoginActivity.this, "登录成功");
				String userName = jsonObject.getString("USERNAME");
				String sex = jsonObject.getString("SEX");
				String province = jsonObject.getString("PROVINCE");
				String city = jsonObject.getString("CITY");
				String county = jsonObject.getString("COUNTY");
				String location = jsonObject.getString("LOCATION");
//				ldPref.setUserName(userName);
				MyLog.d("weipeipei", "userName = " + userName);
//				ldPref.setUserMobile(strPhone);
//				ldPref.setUserPswd(pswd);
//				ldPref.setUserSex(sex);
//				ldPref.setUserProvince(province);
//				ldPref.setUserCity(city);
//				ldPref.setUserCounty(county);
//				ldPref.setUserLocation(location);
//				setResult(RESULT_OK);
//				LoginActivity.this.finish();
			}else{
//				MyToast.showToast(LoginActivity.this, info);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
