Android開發之登入驗證,android開發驗證
最近在做一個小項目,項目開發中需要實現一個登入驗證功能,具體的要求就是,在Android端輸入使用者名稱和密碼,在伺服器端驗證MySQL資料庫中是否有此使用者,實現之前當然首要的是,如何使Android端的資料發送到伺服器端,具體的實現方法:
伺服器端:ManageServlet.java
public class ManageServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");String name = request.getParameter("name");String password = request.getParameter("password");System.out.println("使用者名稱:"+name+" 密碼:"+password);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}
在這裡實現的僅僅是把使用者端的資料在控制台列印出來,相信學過jsp開發的大神,剩下的資料驗證應該不在話下,在此不再贅述。
接下來就是Android端了:
主activity:MainActivity.java
public class MainActivity extends Activity {private EditText textname = null;private EditText textpassword = null;private Button button = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {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(new mybuttonlistener());}class mybuttonlistener implements OnClickListener{boolean result=false;String name;String password;public void onClick(View v) {try {name = textname.getText().toString();name = new String(name.getBytes("ISO8859-1"), "UTF-8");password = textpassword.getText().toString();password = new String(password.getBytes("ISO8859-1"), "UTF-8");} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {result = NewsService.save(name,password);} catch (Exception e) {// TODO Auto-generated catch blocke.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();}}}}
布局檔案:
<RelativeLayout xmlns: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):
public class NewsService {/** * 登入驗證 * @param name 姓名 * @param password 密碼 * @return */public static boolean save(String name, String password){String path = "http://192.168.1.104:8080/Register/ManageServlet"; Map<String, String> student = new HashMap<String, String>();student.put("name", name);student.put("password", password);try {return SendGETRequest(path, student, "UTF-8");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}/** * 發送GET請求 * @param path 請求路徑 * @param student 請求參數 * @return 請求是否成功 * @throws Exception */private static boolean SendGETRequest(String path, Map<String, String> student, String ecoding) throws Exception{// http://127.0.0.1:8080/Register/ManageServlet?name=1233&password=abcStringBuilder url = new StringBuilder(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);HttpsURLConnection conn = (HttpsURLConnection)new URL(url.toString()).openConnection();conn.setConnectTimeout(100000);conn.setRequestMethod("GET");if(conn.getResponseCode() == 200){return true;}return false;}}
紅色的為自己電腦的IP地址。
因為需要串連網路,一定要在AndroidManifest.xml進行網路許可權配置:
<uses-permission android:name="android.permission.INTERNET"/>
寫到這裡基本已經把Android向伺服器端發送資料,給大家分享完畢,如有不對的地方還望指正。
android開發登入驗證問題
註冊用的是插入,登陸用查詢就好。
android程式結構:登陸驗證
其實按返回鍵是關閉當前Activity,按home鍵是進入手機主介面,當前應用程式未關閉,再次開啟應用程式時,呈現的是按home鍵的那個介面。按照你的需求:在Activity中重新onBackPressed()方法,可以捕獲按返回鍵的事件,然後進行處理;其次當使用者登入時,應在本地記錄登入資訊如:使用者名稱,密碼;當使用者再次進入應用程式時其實都是進行了判斷了的,如果登入資訊儲存的有,並且資訊正確(這個是在後台發送非同步請求),當然就直接進入主介面。如果沒有登入資訊記錄,或者密碼錯誤,就跳到登入介面。