Android開發(fā)之登錄驗(yàn)證實(shí)例教程
本文所述實(shí)例源自一個(gè)項(xiàng)目開發(fā)中的登錄驗(yàn)證功能,具體的要求就是,在Android端輸入用戶名和密碼,在服務(wù)器端驗(yàn)證MySQL數(shù)據(jù)庫(kù)中是否有此用戶,實(shí)現(xiàn)之前當(dāng)然首要的是,如何使Android端的數(shù)據(jù)發(fā)送到服務(wù)器端,具體的實(shí)現(xiàn)方法如下:
服務(wù)器端: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 { } }
在這里實(shí)現(xiàn)的僅僅是把用戶端的數(shù)據(jù)在控制臺(tái)打印出來(lái),相信學(xué)過(guò)jsp開發(fā)的大神,剩下的數(shù)據(jù)驗(yàn)證應(yīng)該不在話下,在此不再贅述。
接下來(lái)就是Android端了:
主activity:MainActivity.java頁(yè)面代碼如下:
public class MainActivity extends Activity { private EditText textname = null; private EditText textpassword = null; private Button button = null; @Override protected 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 block e1.printStackTrace(); } try { result = NewsService.save(name,password); } catch (Exception e) { // TODO Auto-generated catch block 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(); } } } }
布局文件如下:
<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>
用于向服務(wù)器端發(fā)送數(shù)據(jù)的service(NewsService):
public class NewsService { /** * 登錄驗(yàn)證 * @param name 姓名 * @param password 密碼 * @return */ public static boolean save(String name, String password){ String path = "http://<span style="color: #ff0000;"><strong>192.168.1.104</strong></span>: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 block e.printStackTrace(); } return false; } /** * 發(fā)送GET請(qǐng)求 * @param path 請(qǐng)求路徑 * @param student 請(qǐng)求參數(shù) * @return 請(qǐng)求是否成功 * @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=abc StringBuilder 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; } }
因?yàn)樾枰B接網(wǎng)絡(luò),一定要在AndroidManifest.xml進(jìn)行網(wǎng)絡(luò)權(quán)限配置:
<uses-permission android:name="android.permission.INTERNET"/>
至此基本已經(jīng)完成Android向服務(wù)器端發(fā)送數(shù)據(jù),希望本文實(shí)例對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android手機(jī)注冊(cè)登錄時(shí)獲取驗(yàn)證碼之后倒計(jì)時(shí)功能(知識(shí)點(diǎn)總結(jié))
- Android實(shí)現(xiàn)登錄注冊(cè)功能封裝
- Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(2)
- Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
- Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(1)
- Android注冊(cè)登錄實(shí)時(shí)自動(dòng)獲取短信驗(yàn)證碼
- Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
- Android開發(fā)之注冊(cè)登錄方法示例
- Android實(shí)現(xiàn)登錄功能demo示例
- Android登錄注冊(cè)功能 數(shù)據(jù)庫(kù)SQLite驗(yàn)證
相關(guān)文章
TextView實(shí)現(xiàn)圖文混合編排的方法
這篇文章主要為大家詳細(xì)介紹了TextView實(shí)現(xiàn)圖文混合編排的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android隱私協(xié)議提示彈窗的實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Android隱私協(xié)議提示彈窗的實(shí)現(xiàn)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Android開發(fā)之ViewPager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之ViewPager實(shí)現(xiàn)滑動(dòng)切換頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法
本文主要介紹 Android OnSharedPreferenceChangeListener的知識(shí),在Android應(yīng)用開發(fā)過(guò)程中會(huì)遇到監(jiān)聽器不觸發(fā)事件問(wèn)題,這里介紹了相應(yīng)的解決辦法2016-08-08自定義ListView實(shí)現(xiàn)拖拽ListItem項(xiàng)交換位置(附源碼)
本文要實(shí)現(xiàn)的是拖拽ListView的Item項(xiàng),在布局方面還是用基于布局泵LayoutInflater來(lái)從不同的Layout模板拿到不同的布局然后將view返回,感興趣的朋友可以了解下哈2013-06-06使用newInstance()來(lái)實(shí)例化fragment并傳遞數(shù)據(jù)操作
這篇文章主要介紹了使用newInstance()來(lái)實(shí)例化fragment并傳遞數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Android中實(shí)現(xiàn)根據(jù)資源名獲取資源ID
這篇文章主要介紹了Android中實(shí)現(xiàn)根據(jù)資源名獲取資源ID,本文講解了使用文件名獲取資源ID的方法,需要的朋友可以參考下2015-01-01Android TreeView實(shí)現(xiàn)帶復(fù)選框樹形組織結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了Android TreeView實(shí)現(xiàn)帶復(fù)選框樹形組織結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Android中AnimationDrawable使用的簡(jiǎn)單實(shí)例
這篇文章介紹了Android中AnimationDrawable使用的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10