标签:
登陆LoginActivity:
package net.shop.activity;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.impl.cookie.BasicClientCookie2;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import net.jiaxiang.R;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Entity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity implements OnClickListener{
private EditText eT_username,eT_password;
private Button bt_login,but_reset_psd,but_register;
//获取文本框的值
private String username,password;
//在主线程中是不能进行修改主屏幕的所以只有写在外面在进行调用
private Handler handler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
if(msg.what==0){
//弹出提示框
Toast.makeText(getApplicationContext(), "用户名和密码不正确!请从新输入!", Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//获取组件
eT_username=(EditText) findViewById(R.id.eT_username);
eT_password=(EditText) findViewById(R.id.eT_password);
//登入商城
bt_login=(Button) findViewById(R.id.but_login);
//忘记密码
but_reset_psd=(Button) findViewById(R.id.but_reset_psd);
//注册商城
but_register=(Button) findViewById(R.id.but_register);
//获取用户名和密码
//获取文本框中的内容 在进行转换成【字符串】类型trim():在去掉空格
username=eT_username.getText().toString().trim();
password=eT_password.getText().toString().trim();
//点击事件
bt_login.setOnClickListener(this);
but_reset_psd.setOnClickListener(this);
but_register.setOnClickListener(this);
//注意程序在访问网络的时候必须是放在子线程中进行运行,因为连接网络和数据库比较慢,不放在子线程中就会造成主线程卡死
new Thread(new Runnable() {
public void run() {
//点击事件
// bt_login.setOnClickListener(this);
// bt_login.setOnClickListener(bt_login);
//调用实现这个网络加载在子进程中进行加载
// onClick(bt_login);
}
}).start();
}
//控件点击事件
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.but_login:
if(username==null || username.length()<=0){
//获取焦点也就是重新让光标在用户名这里
eT_username.requestFocus();
eT_username.setError("用户名不能为空!");
return;
}else if (password==null || password.length()<=0) {
//获取焦点也就是重新让光标在密码这里
eT_password.requestFocus();
eT_password.setError("密码不能为空!");
}
//http通信(向服务器发出请求,并获取响应)
//创建HTTPClient实例
HttpClient hpct=new DefaultHttpClient();
//确定请求方法(post)
HttpPost request=new HttpPost("http://192.168.147.22:8080/LoginServlet");
//使用NameValuePair来保存客户端传递的参数,可以使用BasicNameValuePair来构造一个要被传递的参数,通过add方法把这参数加入到NameValuePair中
List<NameValuePair>nvp=new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("name", username));
nvp.add(new BasicNameValuePair("pwd", password));
try {
//设置一个编码
UrlEncodedFormEntity uefe=new UrlEncodedFormEntity(nvp,"GBK");
request.setEntity(uefe);
//执行
HttpResponse response=hpct.execute(request);
// HttpEntity tpentity=response.getEntity();//方法一
String entity=EntityUtils.toString(request.getEntity());//方法二
//判断entity是“yes”或"no"
if(entity.equals("yes")){
Intent intent=new Intent(LoginActivity.this,MainActivityB.class);
//开启跳转
startActivity(intent);
//关闭线程
LoginActivity.this.finish();
}else{
//发送消息;
Message msg=new Message();
msg.what=0;
//调用方法消息弹出提示框架
handler.sendMessage(msg);
}
//编码异常抛出
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//所有的异常抛出
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//
break;
case R.id.but_register:
break;
case R.id.but_reset_psd:
break;
}
}
@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;
}
}
标签:
原文地址:http://www.cnblogs.com/caidupingblogs/p/5558268.html