android利用servlet获取服务器数据库数据进行登录

来源:互联网 发布:网络营销策划方案例文 编辑:程序博客网 时间:2024/06/05 10:17

客服端代码:


public class MainActivity extends Activity {


private EditText ed_username,ed_password;
private TextView te_login;
private Handler mHandler;
  
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
         ed_username=(EditText) findViewById(R.id.username);
         ed_password=(EditText) findViewById(R.id.password);
         te_login=(TextView) findViewById(R.id.login);
         te_login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
   new AT().start();
   mHandler = new Handler(){
       @Override
       public void handleMessage(Message msg) {
           switch (msg.what){
               case 1:
               Intent intent=new Intent(MainActivity.this,MainMenu.class);
startActivity(intent);
                   break;
               
               default:
                   break;
           }
       }
   };
              
}
});

}
 public class UriAPI {  
    public static final String HTTPCustomer ="http://-------:8080/AndroidServlet/login";
   }
  
class AT extends Thread{
     
    String result="";
    public void run(){



CharSequence  username=ed_username.getText().toString();
CharSequence  password=ed_password.getText().toString();
if(!username.equals("")&&!password.equals("")){
//请求数据
HttpPost httpRequest  = new HttpPost(UriAPI.HTTPCustomer);
//创建参数  
List<NameValuePair> params=new ArrayList<NameValuePair>(); 
params.add(new BasicNameValuePair("username", username.toString()));
params.add(new BasicNameValuePair("password", password.toString()));

try {
//对提交数据进行编码
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
Log.i("Thread", ""+httpResponse.getStatusLine().getStatusCode());
//获取响应服务器的数据
if (httpResponse.getStatusLine().getStatusCode()==200) {
//利用字节数组流和包装的绑定数据
byte[] data =new byte[2048];
//先把从服务端来的数据转化成字节数组
data =EntityUtils. toByteArray((HttpEntity)httpResponse.getEntity());  
//再创建字节数组输入流对象   
ByteArrayInputStream bais = new ByteArrayInputStream(data);  
//绑定字节流和数据包装流   
DataInputStream dis = new DataInputStream(bais);  
 //将字节数组中的数据还原成原来的各种数据类型,代码如下:  
result=new String(dis.readUTF());
if(result.equals("success")){
Message message = mHandler.obtainMessage();
       message.what = 1;
       mHandler.sendMessage(message);  
}

}
} catch(ClientProtocolException e){  
                   e.printStackTrace();  
               }catch(UnsupportedEncodingException e){  
                   e.printStackTrace();  
               } catch (IOException e) {  
                   e.printStackTrace();  
               }  

}

}
   
  
}
}

服务端代码:


public class loginserver extends HttpServlet {
private static final long serialVersionUID = 1L;
     
    public loginserver() {
        super();
       
    }



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
doPost(request, response);

}



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String username = request.getParameter("username");
String password = request.getParameter("password");
String flag="";
DataOutputStream output=new DataOutputStream(response.getOutputStream());
try {

if(checklogin(username, password)){
 flag="success";
 output.writeUTF(flag);
}else{
flag="fail";
output.writeUTF(flag);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Boolean checklogin(String username,String password) throws SQLException{
Jdbcconn jdbcconn=new Jdbcconn();
try {
Connection conn=jdbcconn.getConn();
String sql="select * from user where username=? and password=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
    ResultSet rs=ps.executeQuery();
     if(rs.next()){
     return true;
     }else {
return false;
}

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;




}
}


数据库链接的类:

private static String username="--------";
  private static String password="---";
  private static String driverNmae="com.mysql.jdbc.Driver";
  private static String url="jdbc:mysql://---------:3306/test";
  
  public static Connection getConn() throws ClassNotFoundException{
 
try { 
Connection conn;
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(url,username,password);
return conn;
} catch (SQLException e) {

e.printStackTrace();
return null;

}


  }




阅读全文
0 0
原创粉丝点击