handler的使用一般和线程联系,主线程中不能做的事情,比如打开网页链接,需要新开一个线程。这时问题就出现了,在run方法里,如果对主界面做改动,是不行的,这时候就需要用到handler。比如说我新开一个线程,作为登录界面的登陆是否成功的标志。
这是客户端发送请求的代码。即客户端用来验证用户名和密码是否正确的请求。
package com.example.homework1117;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
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 android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
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 MainActivity extends Activity {
Handler handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what==1){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_LONG).show();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et_name = (EditText) findViewById(R.id.et_name);
final EditText et_pass = (EditText) findViewById(R.id.et_pass);
Button btn_login = (Button) findViewById(R.id.btn_login);
final String Url = "http://10.0.2.2:8080/Homework/userServlet";
btn_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String name = et_name.getText().toString();
final String pass = et_pass.getText().toString();
if(name == null||name.equals("")||pass == null||pass.equals("")){
Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
}else {
new Thread(){
public void run() {
boolean b = login(Url,name,pass);
Message message = new Message();
if (b==true){
handler.sendEmptyMessage(1);
}else {
handler.sendEmptyMessage(2);
}
}
}.start();
}
}
});
}
public boolean login(String url,String name,String pass){
HttpClient httpclient = new DefaultHttpClient();
JSONObject jsonobject = new JSONObject();
try {
jsonobject.put("name", name);
jsonobject.put("pass", pass);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("params", jsonobject.toString()));
HttpGet httpget = new HttpGet(url+"?"+URLEncodedUtils.format(list, "utf-8"));
try {
HttpResponse response = httpclient.execute(httpget);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode+":+++++++++++++++++++++++");
if (statusCode == 200){
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity);
JSONObject jsonobject1 = new JSONObject(string);
String string2 = jsonobject1.getString("login");
boolean b = Boolean.parseBoolean(string2);
return b;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
服务器端的代码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String parameter = request.getParameter("params");
JSONObject jsonobject = JSONObject.fromObject(parameter);
String name = jsonobject.getString("name");
String pass = jsonobject.getString("pass");
UserDAO userdao = new UserDAO();
boolean b = userdao.login(name, pass);
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
JSONObject jsonobject1 = new JSONObject();
String state = null;
if(b == true){
jsonobject1.put("login", true);
state = jsonobject1.toString();
}else {
jsonobject1.put("login", false);
state = jsonobject1.toString();
}
writer.write(state);
}
封装的类以及方法
public boolean login(String name,String pass) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
PreparedStatement ps = conn.prepareStatement("select * from user where name =? and pass = ?");
ps.setString(1, name);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return true;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
这样就完成了登录的验证用户名,密码功能。