首页 > 代码库 > 赵雅智_handler

赵雅智_handler



package com.example.android_http;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.util.StreamTools;

public class LoginActivity extends Activity {
	private EditText et_username;
	private EditText et_password;

	private TextView tv_result;
	private final int CHANGETEXTVIEW = 1;
	
	//消息处理者
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			int what = msg.what;//标识
			switch (what) {
			case CHANGETEXTVIEW://这个消息就改变TextView控件的操作
				String result = (String) msg.obj;//附带的对象
				tv_result.setText(result);//改变控件内容
				Toast.makeText(LoginActivity.this, result, Toast.LENGTH_LONG).show();
				break;

			default:
				break;
			}
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		findView();

	}

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

	public void findView() {
		et_password = (EditText) findViewById(R.id.editpass);
		et_username = (EditText) findViewById(R.id.editusername);
		tv_result = (TextView) findViewById(R.id.tv_result);
	}

	public void login(View v) {
		// 获取点击控件的id
		int id = v.getId();
		// 根据id进行判断
		switch (id) {
		case R.id.btn_login:// 进行登录操作
			// Toast.makeText(getApplicationContext(), "--", 0).show();
			// 获取用户名密码
			final String userName = et_username.getText().toString();
			final String userPass = et_password.getText().toString();

			// 判断是否为空
			if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPass)) {
				Toast.makeText(getApplicationContext(), "用户名或者密码不能为空", 0)
						.show();
			} else {
				Toast.makeText(getApplicationContext(), "發送請求道服務器", 0)
				.show();
				// 访问网络(需要一个网络的权限)
				// 访问网络(好事操作)子线程,避免阻塞主线程UI
				// http://172.16.237.200:8080/video/login.do?username=chj&userpass=123

				// 所有耗時操作都要在子線程中
				new Thread() {
					public void run() {
						try {
							// 请求地址
							String spec = "http://172.16.237.200:8080/video/login.do?username="
									+ userName + "&userpass=" + userPass;
							// 根据地址创建URL对象(网络访问url)
							URL url = new URL(spec);
							// 采用http协议打开的连接对象
							HttpURLConnection urlConnection = (HttpURLConnection) url
									.openConnection();
							urlConnection.setRequestMethod("GET");// 以get方式发起请求
							urlConnection.setReadTimeout(5000);// 设置超时
							urlConnection.setConnectTimeout(5000);// 设置连接超时
							urlConnection
									.setRequestProperty("User-Agent",
											"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0");
							// 获取相应的 code 400 200 505 302
							if (urlConnection.getResponseCode() == 200) {
								// 得到网络返回的输入流
								InputStream is = urlConnection.getInputStream();
								//通过工具类处理
								String result = StreamTools.streamToStr(is);
								
								Message msg = new Message();
								msg.what = CHANGETEXTVIEW;//消息的唯一标志
								msg.obj = result;//改变的内容通过object传过去
								
								handler.sendMessage(msg);//发送消息
								
								System.out.println("返回的数据是:" + result);
							} else {
								System.out.println("请求url失败");
							}

						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					};
				}.start();

			}
			break;

		default:
			break;
		}
	}
}