package com.smart.gtstrvl;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.example.mountaintravel.base.BaseActivity;
import com.example.mountaintravel.service.MyGPSservice;
import com.example.mountaintravel.util.AesTerminalUtil;
import com.example.mountaintravel.util.LogUtil;
import com.example.mountaintravel.util.MD5Util;
import com.example.mountaintravel.util.PhoneUtil;
import com.example.mountaintravel.util.StreamTools;
import com.example.mountaintravel.util.ToastUtil;
import com.example.mountaintravel.variable.Variable;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PhoneRegisterActivity extends BaseActivity implements
		OnClickListener {
	private ImageView backlogin_iv;
	private EditText register_editTextphone;
	private EditText register_editTextcode;
	private EditText register_editTextpass;
	private EditText register_editTextpassagain;
	private EditText register_editTextname;
	private Button register_button;
	private CheckBox register_checkBox;
	private TextView register_getcode;
	private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
	private String time = format.format(new Date());
	private String type = "android";
	private String key = ".5Xf_01KXd352s177Jx3wxXc074poII";
	private String sign = MD5Util.MD5(time + type + key);
	private String code;
	private TextView register_cancle;
	private static final String TAG = "PhoneLoginActivity";
	private int second;
	private String timer;
	private TextView register_tv;
	private String status = "";
	TextView register_tverr;
	TextView register_tverrtwo;
	TextView register_tverrfour;
	TextView register_tverrfive;
	TextView register_tverrthree;
	TextView register_tverrsix;
	TextView box_tv;
	boolean ifphone = false;
	private SharedPreferences sp;
	private JSONObject json=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_phone_login);
		sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
		backlogin_iv = (ImageView) findViewById(R.id.phoneregister_ivback);
		backlogin_iv.setOnClickListener(this);
		register_tverr = (TextView) findViewById(R.id.register_tverr);
		register_tverrtwo = (TextView) findViewById(R.id.register_tverrtwo);
		register_tverrfour = (TextView) findViewById(R.id.register_tverrfour);
		register_tverrfive = (TextView) findViewById(R.id.register_tverrfive);
		register_tverrthree = (TextView) findViewById(R.id.register_tverrthree);
		register_tverrsix=(TextView) findViewById(R.id.register_tverrsix);
		register_editTextphone = (EditText) findViewById(R.id.register_editTextphone);
		register_editTextphone.requestFocus();
		register_tv = (TextView) findViewById(R.id.register_tv);
		register_tv.setOnClickListener(this);
		box_tv=(TextView) findViewById(R.id.box_tv);
		box_tv.setOnClickListener(this);
		register_editTextcode = (EditText) findViewById(R.id.register_editTextcode);
		register_editTextpass = (EditText) findViewById(R.id.register_editTextpass);
		register_editTextpassagain = (EditText) findViewById(R.id.register_editTextpassagain);
		register_getcode = (TextView) findViewById(R.id.register_getcode);
		register_editTextname = (EditText) findViewById(R.id.register_editTextname);
		register_getcode.setOnClickListener(this);
		register_button = (Button) findViewById(R.id.register_button);
		register_button.setOnClickListener(this);
		register_checkBox = (CheckBox) findViewById(R.id.register_checkBox);
		register_cancle = (TextView) findViewById(R.id.register_cancle);
		
		register_cancle.setOnClickListener(this);
		ifphone = PhoneUtil.isMobileNO(register_editTextphone.getText()
				.toString().trim());

		register_editTextname
				.setOnFocusChangeListener(new View.OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						if (hasFocus) {// 获得焦点
							ifregister_editText1();

						} else {// 失去焦点

						}
					}

				});

		register_editTextcode
				.setOnFocusChangeListener(new View.OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						if (hasFocus) {// 获得焦点
							ifregister_editText1();

							ifregister_editTextname();

						} else {// 失去焦点

						}
					}
				});

		register_editTextpass
				.setOnFocusChangeListener(new View.OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						if (hasFocus) {// 获得焦点
							ifregister_editText1();

							ifregister_editTextname();

							ifregister_editText2();

						} else {// 失去焦点

						}
					}

				});

		register_editTextpassagain
				.setOnFocusChangeListener(new View.OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						if (hasFocus) {// 获得焦点
							ifregister_editText1();

							ifregister_editTextname();

							ifregister_editText2();
							ifregister_editText3();

						} else {// 失去焦点

						}
					}

				});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.phone_login, menu);
		return true;
	}

	private void ifregister_editText3() {
		if (TextUtils
				.isEmpty(register_editTextpass.getText().toString().trim())) {
			register_tverrfour.setVisibility(View.VISIBLE);
			register_tverrfour.setText("  密码不能为空");
			return;
		}

		if (register_editTextpass.getText().toString().trim().length() < 6) {
			register_tverrfour.setVisibility(View.VISIBLE);
			register_tverrfour.setText("  密码长度不能小于6位");
			return;
		}
		register_tverrfour.setVisibility(View.GONE);
	}

	private void ifregister_editText2() {
		if (TextUtils
				.isEmpty(register_editTextcode.getText().toString().trim())) {
			register_tverrthree.setVisibility(View.VISIBLE);
			register_tverrthree.setText("  验证码不能为空");
			return;
		}
		if (register_editTextcode.getText().toString().length() != 4) {
			register_tverrthree.setVisibility(View.VISIBLE);
			register_tverrthree.setText("  验证码长度不合法");
			return;

		}
		register_tverrthree.setVisibility(View.GONE);
	}

	private void ifregister_editTextname() {
		if (register_editTextname.getText().toString().length() > 10
				|| register_editTextname.getText().toString().length() < 2) {
			register_tverrtwo.setVisibility(View.VISIBLE);
			register_tverrtwo.setText("  昵称长度2到10位之间");
			return;

		}
		register_tverrtwo.setText("123");
		register_tverrtwo.setVisibility(View.GONE);
	}

	private void ifregister_editText1() {
		if (TextUtils.isEmpty(register_editTextphone.getText().toString()
				.trim())) {
			register_tverr.setVisibility(View.VISIBLE);
			register_tverr.setText("  号码不能为空");
			return;
		}
		if (register_editTextphone.getText().toString().length() != 11) {
			register_tverr.setVisibility(View.VISIBLE);
			register_tverr.setText("  号码长度不合法");
			return;
			// 有一个接口判断是否已注册过if
		}
		ifphone = PhoneUtil.isMobileNO(register_editTextphone.getText()
				.toString().trim());
		if (!ifphone) {
			register_tverr.setVisibility(View.VISIBLE);
			register_tverr.setText("  号码不存在");
			return;
		}
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				loginByHttpClientGet();
			}
		}).start();
		register_tverr.setText("123");
		register_tverr.setVisibility(View.GONE);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.phoneregister_ivback:
			finish();
			break;
		case R.id.register_tv:
			 startActivity(AgreeActivity.class);
			break;
		case R.id.box_tv:
			if (register_checkBox.isChecked()) {
				register_checkBox.setChecked(false);
			}else{
				register_checkBox.setChecked(true);
			}
			
			break;
		case R.id.register_cancle:
			register_editTextphone.setText("");
			register_editTextcode.setText("");
			register_editTextpass.setText("");
			register_editTextpassagain.setText("");
			register_editTextname.setText("");
			register_tverr.setVisibility(View.GONE);
			register_tverrtwo.setVisibility(View.GONE);
			register_tverrfour.setVisibility(View.GONE);
			register_tverrfive.setVisibility(View.GONE);
			register_tverrthree.setVisibility(View.GONE);
			second = 1;
			register_checkBox.setChecked(false);
			break;
		case R.id.register_button:
			if (TextUtils.isEmpty(register_editTextphone.getText().toString()
					.trim())) {
				register_tverr.setVisibility(View.VISIBLE);
				register_tverr.setText("  号码不能为空");
				return;
			}
			if (register_editTextphone.getText().toString().length() != 11) {
				register_tverr.setVisibility(View.VISIBLE);
				register_tverr.setText("  号码长度不合法");
				return;
				// 有一个接口判断是否已注册过if
			}
			register_tverr.setVisibility(View.GONE);
			if (register_editTextname.getText().toString().length() > 10
					|| register_editTextname.getText().toString().length() < 2) {
				register_tverrtwo.setVisibility(View.VISIBLE);
				register_tverrtwo.setText("  昵称长度2到10位之间");
				return;

			}
			register_tverrtwo.setVisibility(View.GONE);
			if (TextUtils.isEmpty(register_editTextcode.getText().toString()
					.trim())) {
				register_tverrthree.setVisibility(View.VISIBLE);
				register_tverrthree.setText("  验证码不能为空");
				return;
			}
			if (register_editTextcode.getText().toString().length() != 4) {
				register_tverrthree.setVisibility(View.VISIBLE);
				register_tverrthree.setText("  验证码长度不合法");
				return;

			}
			register_tverrthree.setVisibility(View.GONE);
			if (TextUtils.isEmpty(register_editTextpass.getText().toString()
					.trim())) {
				register_tverrfour.setVisibility(View.VISIBLE);
				register_tverrfour.setText("  密码不能为空");
				return;
			}

			if (register_editTextpass.getText().toString().trim().length() < 6) {
				register_tverrfour.setVisibility(View.VISIBLE);
				register_tverrfour.setText("  密码长度不能小于6位");
				return;
			}
			register_tverrfour.setVisibility(View.GONE);
			if (!register_editTextpass.getText().toString().trim().equals(register_editTextpassagain.getText().toString().trim())) {
				register_tverrfive.setVisibility(View.VISIBLE);
				register_tverrfive.setText("  密码不一致");
				return;
			}
			register_tverrfive.setVisibility(View.GONE);
			if (!register_checkBox.isChecked()) {
				register_tverrsix.setVisibility(View.VISIBLE);
				register_tverrsix.setText("  请同意《智慧登山服务协议》");
				return;
			}
			register_tverrsix.setVisibility(View.GONE);
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					register();
				}

			}).start();

			break;
		case R.id.register_getcode:
			if (TextUtils.isEmpty(register_editTextphone.getText().toString()
					.trim())) {
				register_tverr.setVisibility(View.VISIBLE);
				register_tverr.setText("  号码不能为空");
				return;
			}
			if (register_editTextphone.getText().toString().length() != 11) {
				register_tverr.setVisibility(View.VISIBLE);
				register_tverr.setText("  号码长度不合法");
				return;
				// 有一个接口判断是否已注册过if
			}
			ifphone = PhoneUtil.isMobileNO(register_editTextphone.getText()
					.toString().trim());
			if (!ifphone) {
				register_tverr.setVisibility(View.VISIBLE);
				register_tverr.setText("  号码不存在");
				return;
			}
			register_tverr.setText("123");
			register_tverr.setVisibility(View.GONE);
			new Thread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					register_getcode.setOnClickListener(null);
					loginByHttpClientGet();
					if (!register_tverr.getText().toString().equals("  该手机号已注册")) {
						lowgetcode();
						second = 60;
						new MyThread().start();
					} else {
					}

				}
			}).start();

			break;
		}
	}

	private void register() {
		// TODO Auto-generated method stub
		String phone = register_editTextphone.getText().toString().trim();
		String pwds = register_editTextpass.getText().toString().trim();
		String nick_name = register_editTextname.getText().toString().trim();
		DefaultHttpClient client = new DefaultHttpClient();
		String uri = "http://" + Variable.address + "/terminal/register";
		HttpPost httpPost = new HttpPost(uri);
		try {
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			// 往这个集合中添加你要传递的参数
			parameters.add(new BasicNameValuePair("type", type));
			parameters.add(new BasicNameValuePair("time", time));
			parameters.add(new BasicNameValuePair("sign", sign));
			parameters.add(new BasicNameValuePair("name", phone));
			parameters.add(new BasicNameValuePair("login_type", "2"));
			parameters.add(new BasicNameValuePair("pwds", pwds));
			parameters.add(new BasicNameValuePair("nick_name", nick_name));
			// 创建传递参数封装 实体对象
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
					"UTF-8");// 设置传递参数的编码
			// 把实体对象存入到httpPost对象中
			httpPost.setEntity(entity);

			// 3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
			HttpResponse response = client.execute(httpPost); // HttpUriRequest的后代对象
																// //在浏览器中敲一下回车
			if (response.getStatusLine().getStatusCode() == 200) {
				InputStream is = response.getEntity().getContent();
				String result = StreamTools.streamToString(is);
				System.out.println(result);
				JSONObject json = new JSONObject(result);
				status = json.getString("status");
						if (register_editTextcode.getText().toString().trim()
								.equals(code)
								&& status.equals("9002")) {
							
							loginByHttpClientGet(register_editTextphone.getText().toString().trim(),
									register_editTextpass.getText().toString().trim());
							return;
						} 
						runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								if (!register_editTextcode.getText().toString()
										.trim().equals(code)) {
									
									register_tverrthree.setVisibility(View.VISIBLE);
									register_tverrthree.setText("  验证码不正确");
									return;
								} else if (!status.equals("9002")) {
									ToastUtil.show(PhoneRegisterActivity.this, "注册失败");
								}

							}
						});
						
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					ToastUtil.show(PhoneRegisterActivity.this, "注册失败");
				}
			});
		} finally {
			// 6. 释放连接。无论执行方法是否成功，都必须释放连接
			client.getConnectionManager().shutdown();
		}
	}
	private void loginByHttpClientGet(final String userName, final String userPass) {
		// TODO Auto-generated method stub
		DefaultHttpClient client = new DefaultHttpClient();
		String uri = "http://"+Variable.address+"/terminal/login";
		HttpPost httpPost = new HttpPost(uri);
		try {
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			// 往这个集合中添加你要传递的参数
			parameters.add(new BasicNameValuePair("type", type));
			parameters.add(new BasicNameValuePair("time", time));
			parameters.add(new BasicNameValuePair("sign", sign));
			parameters.add(new BasicNameValuePair("name", userName));
			parameters.add(new BasicNameValuePair("login_type", "2"));
			parameters.add(new BasicNameValuePair("pwds", userPass));
			parameters.add(new BasicNameValuePair("origin", "phone"));
			// 创建传递参数封装 实体对象
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
					"UTF-8");// 设置传递参数的编码
			// 把实体对象存入到httpPost对象中
			httpPost.setEntity(entity);
			if ( !Variable.JSESSIONID.equals("")) {
				httpPost.setHeader("Cookie", "JSESSIONID=" + Variable.JSESSIONID);
			}
			// 3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
			HttpResponse response = client.execute(httpPost); // HttpUriRequest的后代对象
																// //在浏览器中敲一下回车
			if (response.getStatusLine().getStatusCode() == 200) {
				CookieStore mCookieStore = client.getCookieStore();
				List<Cookie> cookies = mCookieStore.getCookies();
				for (int i = 0; i < cookies.size(); i++) {
					// 这里是读取Cookie['PHPSESSID']的值存在静态变量中，保证每次都是同一个值
					if ("JSESSIONID".equals(cookies.get(i).getName())) {
						Variable.JSESSIONID = cookies.get(i).getValue();
						break;
					}
				}
				InputStream is = response.getEntity().getContent();
				int b = 0;
				StringBuffer sb = new StringBuffer();
				while ((b = is.read()) != -1) {
					sb.append((char) b);
				}
				String result = sb.toString();
				 json = new JSONObject(result);
				 status = json.getString("status");
					 runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								if (status.equals("9002")) {
									sp.edit().putBoolean("isloginsuccess", true).commit();
									sp.edit().putString("userName", userName).commit();
									sp.edit().putString("userPass", userPass).commit();
									try{
									 JSONObject jsondata=json.getJSONObject("data");
									 String headImageUrl=jsondata.getString("headImgUrl");
									 sp.edit().putString("url", "http://" + Variable.address+headImageUrl).commit();
									 sp.edit().putString("userid", json.getString("userid")).commit();
									 sp.edit().putString("sesskey", json.getString("sesskey")).commit();
										Map<String,Object> map = AesTerminalUtil.gtsApiAesDecryptToMap(json.getString("sesskey"));
										String real_name=(String) map.get("real_name");
										sp.edit().putString("real_name", real_name).commit();
										sp.edit().putString("nick_name", (String) map.get("nick_name")).commit();
										sp.edit().putString("increased", (String) map.get("increased")).commit();
										sp.edit().putString("weight", (String) map.get("weight")).commit();
										String aa=(String) map.get("device_sn");
										String sex1=(String) map.get("sex");
										if (sex1.equals("0")) {
											sp.edit().putString("sex", "女").commit();
										} else {
											sp.edit().putString("sex", "男").commit();
										}
										ToastUtil.show(PhoneRegisterActivity.this, "注册成功");
										if (!isWorked("com.example.mountaintravel.service.MyGPSservice")) {
											Intent startIntent = new Intent(PhoneRegisterActivity.this, MyGPSservice.class);
											startService(startIntent);
										}
										if (sp.getBoolean("comment", false)) {
											sp.edit().putBoolean("comment", false).commit();
											finish();
											return;
										}
										
										startActivity(MainActivity.class);
									}catch(Exception e){
										e.printStackTrace();
										
									}
									
								}
									
							}
						});
					 
				
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} finally {
			// 6. 释放连接。无论执行方法是否成功，都必须释放连接
			client.getConnectionManager().shutdown();
		}

	}
	private boolean isWorked(String className) {
		ActivityManager myManager = (ActivityManager) PhoneRegisterActivity.this
				.getApplicationContext().getSystemService(
						Context.ACTIVITY_SERVICE);
		ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
				.getRunningServices(30);
		for (int i = 0; i < runningService.size(); i++) {
			if (runningService.get(i).service.getClassName().toString()
					.equals(className)) {
				return true;
			}
		}
		return false;
	}
	final Handler handler = new Handler() { // handle
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				String timerbegin = msg.obj.toString();
				register_getcode.setText(timerbegin);
				register_getcode.setOnClickListener(null);
				if (second == 0) {
					register_getcode
							.setOnClickListener(PhoneRegisterActivity.this);
					register_getcode.setText("获取验证码");
				}

			}
			super.handleMessage(msg);
		}
	};

	public class MyThread extends Thread {
		public void run() {
			while (second > 0) {
				try {
					timer = second + "秒";

					MyThread.sleep(1000);

					second -= 1;
					Message message = new Message();
					message.obj = timer;
					message.what = 1;
					handler.sendMessage(message);
				}

				catch (Exception e) {
				}
			}
			if (second == 0) {
				try {
					MyThread.sleep(1000);
					Message message = new Message();
					message.obj = timer;
					message.what = 1;
					handler.sendMessage(message);
				} catch (Exception e) {
				}

			}
		}
	}

	private void lowgetcode() {
		// TODO Auto-generated method stub
		String phone = register_editTextphone.getText().toString().trim();
		DefaultHttpClient client = new DefaultHttpClient();
		// String uri = "http://192.168.20.80:5050/gts/terminal/login";
		String uri = "http://" + Variable.address + "/terminal/smsapi";
		HttpPost httpPost = new HttpPost(uri);
		try {
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			// 往这个集合中添加你要传递的参数

			parameters.add(new BasicNameValuePair("type", type));
			parameters.add(new BasicNameValuePair("time", time));
			parameters.add(new BasicNameValuePair("sign", sign));
			parameters.add(new BasicNameValuePair("phone", phone));
			// 创建传递参数封装 实体对象
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
					"UTF-8");// 设置传递参数的编码
			// 把实体对象存入到httpPost对象中
			httpPost.setEntity(entity);
			// 3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
			HttpResponse response = client.execute(httpPost); // HttpUriRequest的后代对象
																// //在浏览器中敲一下回车
			if (response.getStatusLine().getStatusCode() == 200) {
				InputStream is = response.getEntity().getContent();
				String result = StreamTools.streamToString(is);
				System.out.println(result);
				JSONObject json = new JSONObject(result);
				
				JSONObject msgjson = json.getJSONObject("data");
				code = msgjson.getString("verifycode");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			client.getConnectionManager().shutdown();
		}
	}

	private void loginByHttpClientGet() {
		// TODO Auto-generated method stub
		String phone = register_editTextphone.getText().toString().trim();
		DefaultHttpClient client = new DefaultHttpClient();
		String uri = "http://" + Variable.address + "/terminal/phoneapi";
		HttpPost httpPost = new HttpPost(uri);
		try {
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			// 往这个集合中添加你要传递的参数
			parameters.add(new BasicNameValuePair("type", type));
			parameters.add(new BasicNameValuePair("time", time));
			parameters.add(new BasicNameValuePair("sign", sign));
			parameters.add(new BasicNameValuePair("phone", phone));
			// 创建传递参数封装 实体对象
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
					"UTF-8");// 设置传递参数的编码
			// 把实体对象存入到httpPost对象中
			httpPost.setEntity(entity);
			if ( !Variable.JSESSIONID.equals("")) {
				httpPost.setHeader("Cookie", "JSESSIONID="
						+ Variable.JSESSIONID);
			}
			// 3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
			HttpResponse response = client.execute(httpPost); // HttpUriRequest的后代对象
																// //在浏览器中敲一下回车
			if (response.getStatusLine().getStatusCode() == 200) {
				CookieStore mCookieStore = client.getCookieStore();
				List<Cookie> cookies = mCookieStore.getCookies();
				for (int i = 0; i < cookies.size(); i++) {
					// 这里是读取Cookie['PHPSESSID']的值存在静态变量中，保证每次都是同一个值
					if ("JSESSIONID".equals(cookies.get(i).getName())) {
						Variable.JSESSIONID = cookies.get(i).getValue();
						break;
					}
				}
				InputStream is = response.getEntity().getContent();
				int b = 0;
				StringBuffer sb = new StringBuffer();
				while ((b = is.read()) != -1) {
					sb.append((char) b);
				}
				String result = sb.toString();
				JSONObject json = new JSONObject(result);
				String code = json.getString("code");
				if (code.equals("4025")) {
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							register_tverr.setVisibility(View.VISIBLE);
							register_tverr.setText("  该手机号已注册");
						}
					});

				} else {
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							register_tverr.setVisibility(View.GONE);

						}
					});

				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 6. 释放连接。无论执行方法是否成功，都必须释放连接
			client.getConnectionManager().shutdown();
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
	}
}
