android-servlet-mysql实现登录注册功能
Posted 梦想成大牛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android-servlet-mysql实现登录注册功能相关的知识,希望对你有一定的参考价值。
安卓项目图:
安卓端Get请求服务端登录代码:
1 package com.example.kkkkkkkkk; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.Serializable; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 import org.json.JSONObject; 13 import com.example.kkkkkkkkk.StreamTools; 14 import android.support.v7.app.ActionBarActivity; 15 import android.text.TextUtils; 16 import android.app.Activity; 17 import android.content.Intent; 18 import android.os.Bundle; 19 import android.os.Handler; 20 import android.os.Message; 21 import android.view.Menu; 22 import android.view.MenuItem; 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.widget.Button; 26 import android.widget.EditText; 27 import android.widget.Toast; 28 29 public class MainActivity extends Activity { 30 protected static final int ERROR = 1; 31 protected static final int SUCCESS = 2; 32 protected static final int SUCCESSS = 0; 33 BufferedReader bufferReader; 34 private EditText account; 35 private EditText password; 36 private Button register; 37 private Handler handler=new Handler(){ 38 public void handleMessage(android.os.Message msg){ 39 switch(msg.what){ 40 case SUCCESS: 41 Toast.makeText(MainActivity.this,(String)msg.obj, 1).show(); 42 break; 43 case ERROR: 44 Toast.makeText(MainActivity.this,"发送失败", 1).show(); 45 break; 46 } 47 }; 48 }; 49 protected void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 setContentView(R.layout.activity_main); 52 account = (EditText)findViewById(R.id.account); 53 password=(EditText)findViewById(R.id.password); 54 register = (Button)findViewById(R.id.login); 55 } 56 public void register(View view){ 57 Intent intent=new Intent(this, RegisterActivity.class); 58 startActivity(intent); 59 } 60 public void login(View view){ 61 final String qq=account.getText().toString().trim(); 62 final String pwd=password.getText().toString().trim(); 63 if(TextUtils.isEmpty(qq)){ 64 Toast.makeText(this,"用户名为空登录失败", 0).show(); 65 return; 66 } 67 if(TextUtils.isEmpty(pwd)){ 68 Toast.makeText(this,"密码为空登陆失败", 0).show(); 69 return; 70 } 71 new Thread(){ 72 Map<String, Object> listItem = new HashMap<String, Object>(); 73 List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); 74 public void run(){ 75 try{ 76 String path="http://192.168.1.4:8080/xianfengYan/LoginAction?username="+qq+"&pswd="+pwd; 77 URL url=new URL(path); 78 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 79 conn.setRequestMethod("GET"); 80 conn.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; KB974487)"); 81 int code=conn.getResponseCode(); 82 if(code==200){ 83 InputStream is=conn.getInputStream(); 84 String result=StreamTools.readInputStream(is); 85 if(!result.equals("用户名不存在请重新输入,登陆失败")&&!result.equals("密码错误,登陆失败")){ 86 JSONObject demoJson = new JSONObject(result); 87 Intent intent=new Intent(MainActivity.this,Activity01.class); 88 intent.putExtra("用户名",demoJson.getString("用户名")); 89 System.out.println(demoJson.getString("用户名")); 90 intent.putExtra("密码",demoJson.getString("密码")); 91 System.out.println(demoJson.getString("密码")); 92 intent.putExtra("真实姓名",demoJson.getString("真实姓名")); 93 System.out.println(demoJson.getString("真实姓名")); 94 intent.putExtra("性别",demoJson.getString("性别")); 95 System.out.println(demoJson.getString("性别")); 96 //bundle.putSerializable("hh", (Serializable) msg.obj); 97 //intent.putExtras(bundle); 98 99 100 startActivity(intent); 101 }else{ 102 Message msg=Message.obtain(); 103 msg.what=SUCCESS; 104 msg.obj=result; 105 handler.sendMessage(msg); 106 } 107 }else{ 108 Message msg=Message.obtain(); 109 msg.what=ERROR; 110 handler.sendMessage(msg); 111 } 112 }catch(Exception e){ 113 e.printStackTrace(); 114 Message msg=Message.obtain(); 115 msg.what=ERROR; 116 handler.sendMessage(msg); 117 } 118 }; 119 }.start(); 120 121 } 122 }
安卓端Get请求注册代码:
1 package com.example.kkkkkkkkk; 2 3 import java.io.InputStream; 4 import java.io.UnsupportedEncodingException; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 import java.net.URLEncoder; 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.os.Handler; 12 import android.os.Message; 13 import android.text.TextUtils; 14 import android.view.View; 15 import android.widget.Button; 16 import android.widget.EditText; 17 import android.widget.RadioButton; 18 import android.widget.RadioGroup; 19 import android.widget.Toast; 20 import android.widget.RadioGroup.OnCheckedChangeListener; 21 import com.example.kkkkkkkkk.MainActivity; 22 import com.example.kkkkkkkkk.StreamTools; 23 public class RegisterActivity extends Activity implements OnCheckedChangeListener{ 24 protected static final int ERROR = 1; 25 protected static final int SUCCESS = 2; 26 private EditText et_pwd; 27 private EditText et_qq; 28 private EditText et_name; 29 private EditText et_apwd; 30 private RadioButton radio0; 31 private RadioButton radio1; 32 private Button Button1; 33 private RadioGroup rg; 34 String temp=""; 35 private Handler handler=new Handler(){ 36 public void handleMessage(android.os.Message msg){ 37 switch(msg.what){ 38 case SUCCESS: 39 Toast.makeText(RegisterActivity.this,(String)msg.obj, 1).show(); 40 if(msg.obj.equals("注册成功")){ 41 //System.out.println("1111"+msg.obj); 42 Intent intent=new Intent(RegisterActivity.this,MainActivity.class); 43 startActivity(intent); 44 } 45 break; 46 case ERROR: 47 Toast.makeText(RegisterActivity.this,"登录失败", 1).show(); 48 break; 49 } 50 }; 51 }; 52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.activity_register); 55 et_qq = (EditText)findViewById(R.id.et_qq); 56 et_pwd=(EditText)findViewById(R.id.et_pwd); 57 et_name=(EditText)findViewById(R.id.et_name); 58 et_apwd=(EditText)findViewById(R.id.et_apwd); 59 rg=(RadioGroup) findViewById(R.id.radioGroup1) ; 60 rg.setOnCheckedChangeListener(this); 61 Button1=(Button)findViewById(R.id.button1); 62 63 } 64 public void Button1(View view){ 65 Intent intent=new Intent(this, MainActivity.class); 66 startActivity(intent); 67 } 68 public void onCheckedChanged(RadioGroup group,int checkedId){ 69 switch(checkedId){ 70 case R.id.radio0: 71 temp="男"; 72 break; 73 case R.id.radio1: 74 temp="女"; 75 break; 76 } 77 78 } 79 80 public void regin(View view) throws UnsupportedEncodingException{ 81 82 final String qq=et_qq.getText().toString().trim(); 83 final String pwd=et_pwd.getText().toString().trim(); 84 final String name=et_name.getText().toString().trim(); 85 final String apwd=et_apwd.getText().toString().trim(); 86 final String tem =URLEncoder.encode(URLEncoder.encode(temp, "UTF-8"), "UTF-8"); 87 if(TextUtils.isEmpty(qq)){ 88 Toast.makeText(this,"用户名不能为空", 0).show(); 89 return; 90 } 91 if(TextUtils.isEmpty(pwd)){ 92 Toast.makeText(this,"密码不能为空", 0).show(); 93 return; 94 } 95 if(TextUtils.equals(pwd, apwd)==false){ 96 Toast.makeText(this,"两次输入密码不同", 0).show(); 97 return; 98 } 99 if(pwd.length()<6){ 100 Toast.makeText(this,"密码位数小于6安全等级太低", 0).show(); 101 return; 102 } 103 if(temp==""){ 104 Toast.makeText(this,"请选择性别", 0).show(); 105 return; 106 } 107 new Thread(){ 108 public void run(){ 109 try{ 110 String path="http://192.168.1.4:8080/xianfengYan/RegisterAction?username="+qq+"&realname="+name+"&pswd="+pwd+"&sex="+tem; 111 URL url=new URL(path); 112 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 113 conn.setRequestMethod("GET"); 114 conn.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; KB974487)"); 115 int code=conn.getResponseCode(); 116 if(code==200){ 117 InputStream is=conn.getInputStream(); 118 String result=StreamTools.readInputStream(is); 119 Message msg=Message.obtain(); 120 msg.what=SUCCESS; 121 msg.obj=result; 122 handler.sendMessage(msg); 123 }else{ 124 Message msg=Message.obtain(); 125 msg.what=ERROR; 126 handler.sendMessage(msg); 127 } 128 }catch(Exception e){ 129 e.printStackTrace(); 130 Message msg=Message.obtain(); 131 msg.what=ERROR; 132 handler.sendMessage(msg); 133 } 134 }; 135 }.start(); 136 } 137 }
输入流工具类:
1 package com.example.kkkkkkkkk; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.InputStream; 5 6 public class StreamTools { 7 // 把输入流的内容 转化成 字符串 8 public static String readInputStream(InputStream is) { 9 try { 10 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 11 int len = 0; 12 byte[] buffer = new byte[1024]; 13 while ((len = is.read(buffer)) != -1) { 14 baos.write(buffer, 0, len); 15 } 16 is.close(); 17 baos.close(); 18 byte[] result = baos.toByteArray(); 19 // 试着解析 result 里面的字符串. 20 String temp = new String(result); 21 return temp; 22 } catch (Exception e) { 23 e.printStackTrace(); 24 return "获取失败"; 25 } 26 } 27 }
安卓端登录成功返回界面:返回用户的注册信息
1 package com.example.kkkkkkkkk; 2 3 import java.io.Serializable; 4 5 import org.json.JSONObject; 6 7 import android.app.Activity; 8 import android.content.Intent; 9 import android.os.Bundle; 10 import android.widget.EditText; 11 import android.widget.TextView; 12 13 public class Activity01 extends Activity{ 14 15 private TextView tv_username; 16 private TextView tv_realname; 17 private TextView tv_password; 18 private TextView tv_sex; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 // TODO Auto-generated method stub 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_01); 24 //提取数据 25 Intent intent=getIntent(); 26 // Bundle bundle = intent.getExtras(); 27 //Serializable name=bundle.getSerializable("hh"); 28 String name1=intent.getStringExtra("用户名"); 29 String name2=intent.getStringExtra("密码"); 30 String name3=intent.getStringExtra("真实姓名"); 31 String name4=intent.getStringExtra("性别"); 32 //JSONObject json = JSONObject.fromObject(name); 33 34 tv_username=(TextView) findViewById(R.id.tv_username); 35 tv_realname=(TextView) findViewById(R.id.tv_realname); 36 tv_password=(TextView) findViewById(R.id.tv_password); 37 tv_sex=(TextView) findViewById(R.id.tv_sex); 38 tv_username.setText("用户名:"+name1); 39 tv_realname.setText("密码:"+name2); 40 tv_password.setText("真实姓名:"+name3); 41 tv_sex.setText("性别:"+name4); 42 //过滤的方法,在LogCat(deprecated)的Filter中输入--进行过滤,这不是标准的调试方法 43 //System.out.println("--name->>"+name); 44 } 45 }
服务端项目图:
数据库连接类:
1 package com.product.jdbc.dbutil; 2 3 import java.lang.reflect.Field; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.ResultSetMetaData; 9 import java.sql.SQLException; 10 import java.sql.Statement; 11 import java.util.ArrayList; 12 import java.util.HashMap; 13 import java.util.List; 14 import java.util.Map; 15 16 public class JdbcUtils { 17 18 // 表示定义数据库的用户名 19 private final String USERNAME = "root"; 20 // 定义数据库的密码 21 private final String PASSWORD = "123"; 22 // 定义数据库的驱动信息 23 private final String DRIVER = "com.mysql.jdbc.Driver"; 24 // 定义访问数据库的地址 25 private final String URL = "jdbc:mysql://localhost:3306/jdbc_db"; 26 // 定义数据库的链接 27 private Connection connection; 28 // 定义sql语句的执行对象 29 private PreparedStatement pstmt; 30 // 定义查询返回的结果集合 31 private ResultSet resultSet; 32 // 实现批处理操作的功能 33 private Statement stmt; 34 35 以上是关于android-servlet-mysql实现登录注册功能的主要内容,如果未能解决你的问题,请参考以下文章