Android開發(fā)之登錄驗證實例教程
本文所述實例源自一個項目開發(fā)中的登錄驗證功能,具體的要求就是,在Android端輸入用戶名和密碼,在服務(wù)器端驗證MySQL數(shù)據(jù)庫中是否有此用戶,實現(xiàn)之前當然首要的是,如何使Android端的數(shù)據(jù)發(fā)送到服務(wù)器端,具體的實現(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 {
}
}
在這里實現(xiàn)的僅僅是把用戶端的數(shù)據(jù)在控制臺打印出來,相信學過jsp開發(fā)的大神,剩下的數(shù)據(jù)驗證應該不在話下,在此不再贅述。
接下來就是Android端了:
主activity:MainActivity.java頁面代碼如下:
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 {
/**
* 登錄驗證
* @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請求
* @param path 請求路徑
* @param student 請求參數(shù)
* @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=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;
}
}
因為需要連接網(wǎng)絡(luò),一定要在AndroidManifest.xml進行網(wǎng)絡(luò)權(quán)限配置:
<uses-permission android:name="android.permission.INTERNET"/>
至此基本已經(jīng)完成Android向服務(wù)器端發(fā)送數(shù)據(jù),希望本文實例對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android隱私協(xié)議提示彈窗的實現(xiàn)流程詳解
這篇文章主要介紹了Android隱私協(xié)議提示彈窗的實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01
Android開發(fā)之ViewPager實現(xiàn)滑動切換頁面
這篇文章主要為大家詳細介紹了Android開發(fā)之ViewPager實現(xiàn)滑動切換頁面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09
OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法
本文主要介紹 Android OnSharedPreferenceChangeListener的知識,在Android應用開發(fā)過程中會遇到監(jiān)聽器不觸發(fā)事件問題,這里介紹了相應的解決辦法2016-08-08
自定義ListView實現(xiàn)拖拽ListItem項交換位置(附源碼)
本文要實現(xiàn)的是拖拽ListView的Item項,在布局方面還是用基于布局泵LayoutInflater來從不同的Layout模板拿到不同的布局然后將view返回,感興趣的朋友可以了解下哈2013-06-06
使用newInstance()來實例化fragment并傳遞數(shù)據(jù)操作
這篇文章主要介紹了使用newInstance()來實例化fragment并傳遞數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android中實現(xiàn)根據(jù)資源名獲取資源ID
這篇文章主要介紹了Android中實現(xiàn)根據(jù)資源名獲取資源ID,本文講解了使用文件名獲取資源ID的方法,需要的朋友可以參考下2015-01-01
Android TreeView實現(xiàn)帶復選框樹形組織結(jié)構(gòu)
這篇文章主要為大家詳細介紹了Android TreeView實現(xiàn)帶復選框樹形組織結(jié)構(gòu),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Android中AnimationDrawable使用的簡單實例
這篇文章介紹了Android中AnimationDrawable使用的簡單實例,有需要的朋友可以參考一下2013-10-10

