Android开发 httpClient post方法请求tomcat服务器实现注册登陆

来源:互联网 发布:知乎 东邪西毒讲的什么 编辑:程序博客网 时间:2024/06/06 02:40


package com.example.httplogintest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
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.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.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
 private Button register_bn;
 private Button login_bn;
 private EditText user_et;
 private EditText pwd_et;
 private TextView tv1;
 private int username_ok=1;//注册成功
 private int username_already_exit=2;//注册失败,用户名已存在
 private int register_failed=0;//注册失败
 private int login_success=3;//登录成功
 private int login_failed=4;//登录失败
 private int username_pwd_null=5;//用户名或密码为空
 Myhandler myHandler=new Myhandler();
 String basehttpurl="http://192.168.0.103:8080/logintest/";
 String subhttpurl;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_login);
  register_bn=(Button)findViewById(R.id.register_bn);
  login_bn=(Button)findViewById(R.id.login_bn);
  user_et=(EditText)findViewById(R.id.user_et);
  pwd_et=(EditText)findViewById(R.id.pwd_et);
  tv1=(TextView)findViewById(R.id.tv1);
  Myhandler myHandler=new Myhandler();
  register_bn.setOnClickListener(new RegisterListener());
  login_bn.setOnClickListener(new LoginListener());
 }
 
 public class RegisterListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   subhttpurl="register.jsp";
   Loginthread thread=new Loginthread();
   thread.start();

 }
 }
 public class LoginListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   subhttpurl="login.jsp";
   Loginthread thread=new Loginthread();
   thread.start();
   
  }
  
 }
 public class Myhandler extends Handler{

  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   super.handleMessage(msg);
   if(msg.what==username_ok)
    Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
   if(msg.what==username_already_exit)
    Toast.makeText(LoginActivity.this, "用户名已被使用", Toast.LENGTH_SHORT).show();
   if(msg.what==login_success)
    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); 
   if(msg.what==login_failed)
    Toast.makeText(LoginActivity.this, "请重新输入账号密码", Toast.LENGTH_SHORT).show(); 
   if(msg.what==register_failed)
    Toast.makeText(LoginActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
   if( msg.what==username_pwd_null)
    Toast.makeText(LoginActivity.this, "请输入账号密码", Toast.LENGTH_SHORT).show();   
   
  }
  
  
 }
 public class Loginthread extends Thread{

  @Override
  public void run() {
   
   // TODO Auto-generated method stub
   super.run();
   Message msg=new Message();
   String httpUrl=basehttpurl+subhttpurl;
   
   // 创建HttpPost连接对象
   HttpPost httpRequest=new HttpPost(httpUrl);
   
   // 使用NameValuePair来保存要传递的Post参数(键值对)
   List parameter=new ArrayList();
   String username=user_et.getText().toString().trim();
   String pwd=pwd_et.getText().toString().trim();  
    // 添加要传递的参数
   if(!username.equals("")&&!pwd.equals(""))
   {
   parameter.add(new BasicNameValuePair("username",username));
   parameter.add(new BasicNameValuePair("pwd", pwd));
    // 设置字符集 
         HttpEntity httpentity;
   try {
    httpentity = new UrlEncodedFormEntity(parameter, "gb2312");
      // 请求httpRequset 
          httpRequest.setEntity(httpentity); 
     // 取得HttpClient 
          HttpClient httpClient = new DefaultHttpClient(); 
           // 取得HttpResponse 
          HttpResponse httpResponse = httpClient.execute(httpRequest);        
          // HttpStatus.SC_OK表示连接成功
         
          if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String strResult = EntityUtils.toString(httpResponse.getEntity());
       
            //创建JSONObject
            JSONObject jsonobj=new JSONObject(strResult);
            String Status=jsonobj.getString("Status");        
            if(Status.equals("username_ok"))
            {  msg.what=username_ok;
            System.out.println("1");}
            else if(Status.equals("username_already_exit"))
             msg.what=username_already_exit;
            else if(Status.equals("login_success"))
             msg.what=login_success;
            else if(Status.equals("login_failed"))
             msg.what=login_failed;
          }
          else 
            msg.what=register_failed;
           
    
   } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ParseException 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();
   } 
   }
   
   else
    msg.what=username_pwd_null;
      
   myHandler.sendMessage(msg);
   
   
  }
  
  
 }
 @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;
 }

}

0 0