Android开发之登录验证实例教程
本文所述实例源自一个项目开发中的登录验证功能,具体的要求就是,在Android端输入用户名和密码,在服务器端验证MySQL数据库中是否有此用户,实现之前当然首要的是,如何使Android端的数据发送到服务器端,具体的实现方法如下:
服务器端:ManageServlet.java代码如下:
publicclassManageServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Stringname=request.getParameter("name"); Stringpassword=request.getParameter("password"); System.out.println("用户名:"+name+"密码:"+password); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ } }
在这里实现的仅仅是把用户端的数据在控制台打印出来,相信学过jsp开发的大神,剩下的数据验证应该不在话下,在此不再赘述。
接下来就是Android端了:
主activity:MainActivity.java页面代码如下:
publicclassMainActivityextendsActivity{ privateEditTexttextname=null; privateEditTexttextpassword=null; privateButtonbutton=null; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textname=(EditText)findViewById(R.id.name); textpassword=(EditText)findViewById(R.id.password); button=(Button)findViewById(R.id.button); button.setOnClickListener(newmybuttonlistener()); } classmybuttonlistenerimplementsOnClickListener{ booleanresult=false; Stringname; Stringpassword; publicvoidonClick(Viewv){ try{ name=textname.getText().toString(); name=newString(name.getBytes("ISO8859-1"),"UTF-8"); password=textpassword.getText().toString(); password=newString(password.getBytes("ISO8859-1"),"UTF-8"); }catch(UnsupportedEncodingExceptione1){ //TODOAuto-generatedcatchblock e1.printStackTrace(); } try{ result=NewsService.save(name,password); }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } if(result){ Toast.makeText(MainActivity.this,R.string.ok,Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this,R.string.error,Toast.LENGTH_SHORT).show(); } } } }
布局文件如下:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name"/> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/playname" android:singleLine="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/password"/> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true" android:hint="@string/playpass" android:singleLine="true" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="" android:text="@string/submit" /> </LinearLayout> </RelativeLayout>
用于向服务器端发送数据的service(NewsService):
publicclassNewsService{ /** *登录验证 *@paramname姓名 *@parampassword密码 *@return */ publicstaticbooleansave(Stringname,Stringpassword){ Stringpath="http://<spanstyle="color:#ff0000;"><strong>192.168.1.104</strong></span>:8080/Register/ManageServlet"; Map<String,String>student=newHashMap<String,String>(); student.put("name",name); student.put("password",password); try{ returnSendGETRequest(path,student,"UTF-8"); }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } returnfalse; } /** *发送GET请求 *@parampath请求路径 *@paramstudent请求参数 *@return请求是否成功 *@throwsException */ privatestaticbooleanSendGETRequest(Stringpath,Map<String,String>student,Stringecoding)throwsException{ //http://127.0.0.1:8080/Register/ManageServlet?name=1233&password=abc StringBuilderurl=newStringBuilder(path); url.append("?"); for(Map.Entry<String,String>map:student.entrySet()){ url.append(map.getKey()).append("="); url.append(URLEncoder.encode(map.getValue(),ecoding)); url.append("&"); } url.deleteCharAt(url.length()-1); System.out.println(url); HttpsURLConnectionconn=(HttpsURLConnection)newURL(url.toString()).openConnection(); conn.setConnectTimeout(100000); conn.setRequestMethod("GET"); if(conn.getResponseCode()==200){ returntrue; } returnfalse; } }
因为需要连接网络,一定要在AndroidManifest.xml进行网络权限配置:
<uses-permissionandroid:name="android.permission.INTERNET"/>
至此基本已经完成Android向服务器端发送数据,希望本文实例对大家的Android程序设计有所帮助。