android-servlet-mysql实现登录注册功能

幽沉谢世事,俯默窥唐虞。这篇文章主要讲述android-servlet-mysql实现登录注册功能相关的知识,希望能为你提供帮助。
【android-servlet-mysql实现登录注册功能】安卓项目图:

android-servlet-mysql实现登录注册功能

文章图片

 
安卓端Get请求服务端登录代码:
android-servlet-mysql实现登录注册功能

文章图片
android-servlet-mysql实现登录注册功能

文章图片
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 { 30protected static final int ERROR = 1; 31protected static final int SUCCESS = 2; 32protected static final int SUCCESSS = 0; 33BufferedReader bufferReader; 34private EditText account; 35private EditText password; 36private Buttonregister; 37private Handler handler=new Handler(){ 38public void handleMessage(android.os.Message msg){ 39switch(msg.what){ 40case SUCCESS: 41Toast.makeText(MainActivity.this,(String)msg.obj, 1).show(); 42break; 43case ERROR: 44Toast.makeText(MainActivity.this,"发送失败", 1).show(); 45break; 46} 47}; 48 }; 49 protected void onCreate(Bundle savedInstanceState) { 50super.onCreate(savedInstanceState); 51setContentView(R.layout.activity_main); 52account = (EditText)findViewById(R.id.account); 53password=(EditText)findViewById(R.id.password); 54register = (Button)findViewById(R.id.login); 55 } 56 public void register(View view){ 57Intent intent=new Intent(this, RegisterActivity.class); 58startActivity(intent); 59 } 60 public void login(View view){ 61final String qq=account.getText().toString().trim(); 62final String pwd=password.getText().toString().trim(); 63if(TextUtils.isEmpty(qq)){ 64Toast.makeText(this,"用户名为空登录失败", 0).show(); 65return; 66} 67if(TextUtils.isEmpty(pwd)){ 68Toast.makeText(this,"密码为空登陆失败", 0).show(); 69return; 70} 71new Thread(){ 72Map< String, Object> listItem = new HashMap< String, Object> (); 73List< Map< String, Object> > listItems = new ArrayList< Map< String, Object> > (); 74public void run(){ 75try{ 76String path="http://192.168.1.4:8080/xianfengYan/LoginAction?username="+qq+"& pswd="+pwd; 77URL url=new URL(path); 78HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 79conn.setRequestMethod("GET"); 80conn.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; KB974487)"); 81int code=conn.getResponseCode(); 82if(code==200){ 83InputStream is=conn.getInputStream(); 84String result=StreamTools.readInputStream(is); 85if(!result.equals("用户名不存在请重新输入,登陆失败")& & !result.equals("密码错误,登陆失败")){ 86JSONObject demoJson = new JSONObject(result); 87Intent intent=new Intent(MainActivity.this,Activity01.class); 88intent.putExtra("用户名",demoJson.getString("用户名")); 89System.out.println(demoJson.getString("用户名")); 90intent.putExtra("密码",demoJson.getString("密码")); 91System.out.println(demoJson.getString("密码")); 92intent.putExtra("真实姓名",demoJson.getString("真实姓名")); 93System.out.println(demoJson.getString("真实姓名")); 94intent.putExtra("性别",demoJson.getString("性别")); 95System.out.println(demoJson.getString("性别")); 96//bundle.putSerializable("hh", (Serializable) msg.obj); 97//intent.putExtras(bundle); 98 99 100startActivity(intent); 101}else{ 102Message msg=Message.obtain(); 103msg.what=SUCCESS; 104msg.obj=result; 105handler.sendMessage(msg); 106} 107}else{ 108Message msg=Message.obtain(); 109msg.what=ERROR; 110handler.sendMessage(msg); 111} 112}catch(Exception e){ 113e.printStackTrace(); 114Message msg=Message.obtain(); 115msg.what=ERROR; 116handler.sendMessage(msg); 117} 118}; 119}.start(); 120 121} 122 }

MainActivity.java安卓端Get请求注册代码:
android-servlet-mysql实现登录注册功能

文章图片
android-servlet-mysql实现登录注册功能

文章图片
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{ 24protected static final int ERROR = 1; 25protected static final int SUCCESS = 2; 26private EditText et_pwd; 27private EditText et_qq; 28private EditText et_name; 29private EditText et_apwd; 30private RadioButton radio0; 31private RadioButton radio1; 32private Button Button1; 33private RadioGroup rg; 34String temp=""; 35private Handler handler=new Handler(){ 36public void handleMessage(android.os.Message msg){ 37switch(msg.what){ 38case SUCCESS: 39Toast.makeText(RegisterActivity.this,(String)msg.obj, 1).show(); 40if(msg.obj.equals("注册成功")){ 41//System.out.println("1111"+msg.obj); 42Intent intent=new Intent(RegisterActivity.this,MainActivity.class); 43startActivity(intent); 44} 45break; 46case ERROR: 47Toast.makeText(RegisterActivity.this,"登录失败", 1).show(); 48break; 49} 50}; 51}; 52protected void onCreate(Bundle savedInstanceState) { 53super.onCreate(savedInstanceState); 54setContentView(R.layout.activity_register); 55et_qq = (EditText)findViewById(R.id.et_qq); 56et_pwd=(EditText)findViewById(R.id.et_pwd); 57et_name=(EditText)findViewById(R.id.et_name); 58et_apwd=(EditText)findViewById(R.id.et_apwd); 59rg=(RadioGroup) findViewById(R.id.radioGroup1) ; 60rg.setOnCheckedChangeListener(this); 61Button1=(Button)findViewById(R.id.button1); 62 63} 64public void Button1(View view){ 65Intent intent=new Intent(this, MainActivity.class); 66startActivity(intent); 67} 68public void onCheckedChanged(RadioGroup group,int checkedId){ 69switch(checkedId){ 70case R.id.radio0: 71temp="男"; 72break; 73case R.id.radio1: 74temp="女"; 75break; 76} 77 78} 79 80public void regin(View view) throws UnsupportedEncodingException{ 81 82final String qq=et_qq.getText().toString().trim(); 83final String pwd=et_pwd.getText().toString().trim(); 84final String name=et_name.getText().toString().trim(); 85final String apwd=et_apwd.getText().toString().trim(); 86final String tem =URLEncoder.encode(URLEncoder.encode(temp, "UTF-8"), "UTF-8"); 87if(TextUtils.isEmpty(qq)){ 88Toast.makeText(this,"用户名不能为空", 0).show(); 89return; 90} 91if(TextUtils.isEmpty(pwd)){ 92Toast.makeText(this,"密码不能为空", 0).show(); 93return; 94} 95if(TextUtils.equals(pwd, apwd)==false){ 96Toast.makeText(this,"两次输入密码不同", 0).show(); 97return; 98} 99if(pwd.length()< 6){ 100Toast.makeText(this,"密码位数小于6安全等级太低", 0).show(); 101return; 102} 103if(temp==""){ 104Toast.makeText(this,"请选择性别", 0).show(); 105return; 106} 107new Thread(){ 108public void run(){ 109try{ 110String path="http://192.168.1.4:8080/xianfengYan/RegisterAction?username="+qq+"& realname="+name+"& pswd="+pwd+"& sex="+tem; 111URL url=new URL(path); 112HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 113conn.setRequestMethod("GET"); 114conn.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; KB974487)"); 115int code=conn.getResponseCode(); 116if(code==200){ 117InputStream is=conn.getInputStream(); 118String result=StreamTools.readInputStream(is); 119Message msg=Message.obtain(); 120msg.what=SUCCESS; 121msg.obj=result; 122handler.sendMessage(msg); 123}else{ 124Message msg=Message.obtain(); 125msg.what=ERROR; 126handler.sendMessage(msg); 127} 128}catch(Exception e){ 129e.printStackTrace(); 130Message msg=Message.obtain(); 131msg.what=ERROR; 132handler.sendMessage(msg); 133} 134}; 135}.start(); 136} 137 }

RegisterActivity.java输入流工具类:
android-servlet-mysql实现登录注册功能

文章图片
android-servlet-mysql实现登录注册功能

文章图片
1 package com.example.kkkkkkkkk; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.InputStream; 5 6 public class StreamTools { 7// 把输入流的内容 转化成 字符串 8public static String readInputStream(InputStream is) { 9try { 10ByteArrayOutputStream baos = new ByteArrayOutputStream(); 11int len = 0; 12byte[] buffer = new byte[1024]; 13while ((len = is.read(buffer)) != -1) { 14baos.write(buffer, 0, len); 15} 16is.close(); 17baos.close(); 18byte[] result = baos.toByteArray(); 19// 试着解析 result 里面的字符串. 20String temp = new String(result); 21return temp; 22} catch (Exception e) { 23e.printStackTrace(); 24return "获取失败"; 25} 26} 27 }

StreamTools.java安卓端登录成功返回界面:返回用户的注册信息
android-servlet-mysql实现登录注册功能

文章图片
android-servlet-mysql实现登录注册功能

文章图片
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 15private TextView tv_username; 16private TextView tv_realname; 17private TextView tv_password; 18private TextView tv_sex; 19@Override 20protected void onCreate(Bundle savedInstanceState) { 21// TODO Auto-generated method stub 22super.onCreate(savedInstanceState); 23setContentView(R.layout.activity_01); 24//提取数据 25Intent intent=getIntent(); 26// Bundle bundle = intent.getExtras(); 27//Serializable name=bundle.getSerializable("hh"); 28String name1=intent.getStringExtra("用户名"); 29String name2=intent.getStringExtra("密码"); 30String name3=intent.getStringExtra("真实姓名"); 31String name4=intent.getStringExtra("性别"); 32//JSONObject json = JSONObject.fromObject(name); 33 34tv_username=(TextView) findViewById(R.id.tv_username); 35tv_realname=(TextView) findViewById(R.id.tv_realname); 36tv_password=(TextView) findViewById(R.id.tv_password); 37tv_sex=(TextView) findViewById(R.id.tv_sex); 38tv_username.setText("用户名:"+name1); 39tv_realname.setText("密码:"+name2); 40tv_password.setText("真实姓名:"+name3); 41tv_sex.setText("性别:"+name4); 42//过滤的方法,在LogCat(deprecated)的Filter中输入--进行过滤,这不是标准的调试方法 43//System.out.println("--name-> > "+name); 44} 45}

Activity01.java服务端项目图:
android-servlet-mysql实现登录注册功能

文章图片
   
android-servlet-mysql实现登录注册功能

文章图片
   
 
数据库连接类:
android-servlet-mysql实现登录注册功能

文章图片
android-servlet-mysql实现登录注册功能

文章图片
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// 表示定义数据库的用户名 19private final String USERNAME = "root"; 20// 定义数据库的密码 21private final String PASSWORD = "123"; 22// 定义数据库的驱动信息 23private final String DRIVER = "com.mysql.jdbc.Driver"; 24// 定义访问数据库的地址 25private final String URL = "jdbc:mysql://localhost:3306/jdbc_db"; 26// 定义数据库的链接 27private Connection connection; 28// 定义sql语句的执行对象 29private PreparedStatement pstmt; 30// 定义查询返回的结果集合 31private ResultSet resultSet; 32// 实现批处理操作的功能 33private Statement stmt; 34 35

    推荐阅读