Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > Windows Azure > Windows Azure (Mobile Services) and Android > ตอนที่ 15 : Show Case 2 : Login User Password (Android and Mobile Services)



Clound SSD Virtual Server

ตอนที่ 15 : Show Case 2 : Login User Password (Android and Mobile Services)

ตอนที่ 15 : Show Case 2 : Login User Password (Android and Mobile Services) หลังจากบทความก่อนหน้านี้เราได้ทำระบบ Register Form บน Android และส่งข้อมูลไปเก็บไว้ที่ Mobile Services ไปเรียบร้อยแล้ว บทความนี้จะเป็นการทำระบบ Login Form ด้วย Username และ Password โดยข้อมูลและ Table ถูกจัดเก็บไว้ที่ Mobile Services บน Windows Azure

Android  Login User Password (Android and Mobile Services)

Android Mobile Services Login Form


สำหรับขั้นตอนนั้นก็คือสร้าง Form Activity สำหรับตรวจสอบ Username และ Password และเมื่อ Login ผ่านก็จะทำการ Intent ไปยังอีก Activity เพื่อแสดงข้อมูลของ User นั้น ๆ ที่ได้ทำการ Login เข้ามาในระบบ

Example ตัวอย่างการทำระบบล็อดอิน Member Login Form

Android  Login User Password (Android and Mobile Services)

ตอนนี้เรามี Mobile Services อยู่ 1 รายการ

Android  Login User Password (Android and Mobile Services)

ชื่อตารางว่า MyMember

Android  Login User Password (Android and Mobile Services)

มีข้อมูลอยู่ 3 รายการ








กลับมายัง Android Project บนโปรแกรม Eclipse

Android  Login User Password (Android and Mobile Services)

เป็นโครงสร้างของไฟล์ทั้งหมด

Android  Login User Password (Android and Mobile Services)

ในไฟล์ AndroidManifrest.xml ให้เพิ่ม DetailActivity เข้าไป

	<activity 
	    android:name="DetailActivity"
	    android:theme="@style/AppTheme"
	    android:screenOrientation="portrait"            
	    android:label="@string/app_name" />


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
   	<TableRow
      android:id="@+id/tableRow1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
     
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Login Form "
        android:layout_span="1"
        android:textAppearance="?android:attr/textAppearanceLarge" />
  	        
 	</TableRow>

	<View
		android:layout_height="1dip"
		android:background="#CCCCCC" />
 
  <TableLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:orientation="horizontal">

      <TextView
          android:id="@+id/textView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Input Username :"
          android:textAppearance="?android:attr/textAppearanceMedium" />

      <EditText
          android:id="@+id/txtUsername"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:ems="10" >
      </EditText>

	<TextView
          android:id="@+id/textView4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Input Password :"
          android:textAppearance="?android:attr/textAppearanceMedium" />

      <EditText
          android:id="@+id/txtPassword"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:ems="10"
          android:inputType="textPassword" >
      </EditText>

		<Button
		    android:id="@+id/btnLogin"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="Login" />
		
  	        
 	</TableLayout >

	<View
		android:layout_height="1dip"
		android:background="#CCCCCC" />
   		  
   	<LinearLayout
      android:id="@+id/LinearLayout1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="5dip" >

   		<TextView
   		    android:id="@+id/textView2"
   		    android:layout_width="wrap_content"
   		    android:layout_height="wrap_content"
   		    android:text="By.. ThaiCreate.Com" />

	</LinearLayout>
	
</TableLayout>


activity_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
   	<TableRow
      android:id="@+id/tableRow1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" >
     
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="User info  "
        android:layout_span="1"
        android:textAppearance="?android:attr/textAppearanceLarge" />
  	        
 	</TableRow>

	<View
		android:layout_height="1dip"
		android:background="#CCCCCC" />
 
  <TableLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:orientation="horizontal">
     
	    <TableRow 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content">
		      <TextView
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="ID : "
		          android:textAppearance="?android:attr/textAppearanceMedium" />
		
		      <TextView
		          android:id="@+id/txtID"
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="ID" />
	    </TableRow>
	    
	    <TableRow 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content">
		      <TextView
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Username : "
		          android:textAppearance="?android:attr/textAppearanceMedium" />
		
		      <TextView
		          android:id="@+id/txtUsername"
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Username" />
	    </TableRow>
	    
	    <TableRow 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content">
		      <TextView
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Password : "
		          android:textAppearance="?android:attr/textAppearanceMedium" />
		
		      <TextView
		          android:id="@+id/txtPassword"
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Password" />
	    </TableRow>
	    
	    <TableRow 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content">
		      <TextView
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Name : "
		          android:textAppearance="?android:attr/textAppearanceMedium" />
		
		      <TextView
		          android:id="@+id/txtName"
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Name" />
	    </TableRow>
	    
	    <TableRow 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content">
		      <TextView
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Email : "
		          android:textAppearance="?android:attr/textAppearanceMedium" />
		
		      <TextView
		          android:id="@+id/txtEmail"
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Email" />
	    </TableRow>
	    
	    <TableRow 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content">
		      <TextView
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Tel : "
		          android:textAppearance="?android:attr/textAppearanceMedium" />
		
		      <TextView
		          android:id="@+id/txtTel"
		          android:layout_width="wrap_content"
		          android:layout_height="wrap_content"
		          android:text="Tel" />
	    </TableRow>

	    <Button
	        android:id="@+id/btnBack"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="Back" />
	    
 	</TableLayout >

	<View
		android:layout_height="1dip"
		android:background="#CCCCCC" />
   		  
   	<LinearLayout
      android:id="@+id/LinearLayout1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="5dip" >

   		<TextView
   		    android:id="@+id/textView2"
   		    android:layout_width="wrap_content"
   		    android:layout_height="wrap_content"
   		    android:text="By.. ThaiCreate.Com" />

	</LinearLayout>
	
</TableLayout>


MyMember.java
package com.example.thaicreate;

public class MyMember {
	
	@com.google.gson.annotations.SerializedName("id")
	private int mId;
	
	@com.google.gson.annotations.SerializedName("username")
	private String mUsername;
	
	@com.google.gson.annotations.SerializedName("password")
	private String mPassword;	
	
	@com.google.gson.annotations.SerializedName("name")
	private String mName;	
	
	@com.google.gson.annotations.SerializedName("tel")
	private String mTel;		
	
	@com.google.gson.annotations.SerializedName("email")
	private String mEmail;	
	
	public MyMember() {
		// empty
	}
	
	public MyMember(String username, String password, 
			String name,String tel,String email) {
		this.setUsername(username);
		this.setPassword(password);
		this.setName(name);
		this.setTel(tel);
		this.setEmail(email);
	}
	
	public int getId() {
		return mId;
	}
	
	public final void setUsername(String username) {
		mUsername = username;
	}
	public String getUsername() {
		return mUsername;
	}
	
	public final void setPassword(String password) {
		mPassword = password;
	}
	public String getPassword() {
		return mPassword;
	}
	
	public final void setName(String name) {
		mName = name;
	}
	public String getName() {
		return mName;
	}	
	
	public final void setTel(String tel) {
		mTel = tel;
	}
	public String getTel() {
		return mTel;
	}	
	
	public final void setEmail(String email) {
		mEmail = email;
	}
	public String getEmail() {
		return mEmail;
	}	
}


MainActivity.java
package com.example.thaicreate;

import java.net.MalformedURLException;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.MobileServiceTable;
import com.microsoft.windowsazure.mobileservices.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.TableQueryCallback;

public class MainActivity extends Activity {
	
	private MobileServiceClient mClient;
	private MobileServiceTable<MyMember> mMyMember;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        /*** Create to Mobile Service ***/
        try {
			mClient = new MobileServiceClient(
				      "https://thaicreate.azure-mobile.net/",
				      "QUjngFknhHZjdaGgYAAzdoXkOzKoxi24",
				      this);
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		/*** Create getTable ***/
		mMyMember = mClient.getTable(MyMember.class);

		final EditText txtUsername = (EditText) findViewById(R.id.txtUsername);
		final EditText txtPassword = (EditText) findViewById(R.id.txtPassword);

		final AlertDialog.Builder adb = new AlertDialog.Builder(this);

		// btnLogin
		final Button btnLogin = (Button) findViewById(R.id.btnLogin);
		// Perform action on click
		btnLogin.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (mClient == null) {
					AlertDialog ad = adb.create();
					ad.setMessage("Cannot connect to Windows Azure Mobile Service!");
					ad.show();
				} else {
					
		      		mMyMember.where().field("username").eq(txtUsername.getText().toString()).and()
        			.field("password").eq(txtPassword.getText().toString())
        			.execute(new TableQueryCallback<MyMember>() {
						public void onCompleted(List<MyMember> result, int count, Exception exception, 
								ServiceFilterResponse response) {
							
							if (exception == null) {
								if(result.size() > 0)
								{
									
									int id = 0;
									/*String username = null;
									String password = null;
									String name = null;
									String tel = null;
									String email = null;*/
					
									MyMember item = result.get(0);
									id = item.getId();
									/*username = item.getUsername();
									password = item.getPassword();
									name = item.getName();
									tel = item.getTel();
									email = item.getEmail();*/
									


									Intent newActivity = new Intent(MainActivity.this,DetailActivity.class);
									newActivity.putExtra("sid", id);
									startActivity(newActivity);

								}
								else
								{
									AlertDialog ad = adb.create();
									ad.setMessage("Incorrect Username and Password!");
									ad.show();
								}
							} else {
								AlertDialog ad = adb.create();
								ad.setMessage("Error : " + exception.getCause().getMessage());
								ad.show();
							}
						}
					});

				}
			}

		});
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


DetailActivity.java
package com.example.thaicreate;

import java.net.MalformedURLException;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.MobileServiceTable;
import com.microsoft.windowsazure.mobileservices.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.TableQueryCallback;

public class DetailActivity extends Activity {
	
	private MobileServiceClient mClient;
	private MobileServiceTable<MyMember> mMyMember;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        
        /*** Create to Mobile Service ***/
        try {
			mClient = new MobileServiceClient(
				      "https://thaicreate.azure-mobile.net/",
				      "QUjngFknhHZjdaGgYAAzdoXkOzKoxi24",
				      this);
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		/*** Create getTable ***/
		mMyMember = mClient.getTable(MyMember.class);
		
		
		Intent intent= getIntent();
		int id = intent.getExtras().getInt("sid"); 
		
		refreshItemsFromTable(id);
     
    }
    
	private void refreshItemsFromTable(final int id) {

		final AlertDialog.Builder adb = new AlertDialog.Builder(this);

		if (mClient == null) {
			AlertDialog ad = adb.create();
			ad.setMessage("Cannot connect to Windows Azure Mobile Service!");
			ad.show();
		} else {
			
      		mMyMember.where().field("id").eq(id).execute(new TableQueryCallback<MyMember>() {
				public void onCompleted(List<MyMember> result, int count, Exception exception, 
						ServiceFilterResponse response) {
					
					if (exception == null) {
						if(result.size() > 0)
						{
							
							String username = null;
							String password = null;
							String name = null;
							String tel = null;
							String email = null;
				            
							MyMember item = result.get(0);
							username = item.getUsername();
							password = item.getPassword();
							name = item.getName();
							email = item.getEmail();
							tel = item.getTel();
							
							final TextView tid = (TextView)findViewById(R.id.txtID);
					    	final TextView tUsername = (TextView)findViewById(R.id.txtUsername);
					    	final TextView tPassword = (TextView)findViewById(R.id.txtPassword);
					    	final TextView tName = (TextView)findViewById(R.id.txtName);
					    	final TextView tEmail = (TextView)findViewById(R.id.txtEmail);
					    	final TextView tTel = (TextView)findViewById(R.id.txtTel);
					    	
					    	tid.setText(String.valueOf(id));
					    	tUsername.setText(username);
					    	tPassword.setText(password);
					    	tName.setText(name);
					    	tEmail.setText(email);
					    	tTel.setText(tel);
							

						}

					} else {
						AlertDialog ad = adb.create();
						ad.setMessage("Error : " + exception.getCause().getMessage());
						ad.show();
					}
				}
			});

		}
		
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


Screensot

Android  Login User Password (Android and Mobile Services)

Login Form บน Android

Android  Login User Password (Android and Mobile Services)

กรณีที่ Login ข้อมูล Username และ Password ผิดพลาด

Android  Login User Password (Android and Mobile Services)

กรณีที่ Login ถูกต้อง


บทความถัดไปที่แนะนำให้อ่าน









บทความที่เกี่ยวข้อง


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-10 12:31:20 / 2017-03-24 11:19:15
  Download : Download  ตอนที่ 15 : Show Case 2 :  Login User Password  (Android and Mobile Services)
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก Android Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : เตรียมความพร้อมของ Android ก่อนที่จะเขียน Android กับ Mobile Services
Rating :

 
ตอนที่ 3 : การสร้าง Android Mobile Services และการเรียกใช้งานแบบง่าย ๆ
Rating :

 
ตอนที่ 4 : การเชื่อมต่อ Android กับ Mobile Services บน Windows Azure
Rating :

 
ตอนที่ 5 : สร้าง Project บน Eclipse การเชื่อมต่อไปยัง Mobile Services ด้วย Android
Rating :

 
ตอนที่ 6 : Android สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล
Rating :

 
ตอนที่ 7 : Android อ่าน Data จาก Table ของ Mobile Services และแสดงผลบน App
Rating :

 
ตอนที่ 8 : อ่านข้อมูล Mobile Services แบบมีเงื่อนไข Where และแสดงผลบน Android
Rating :

 
ตอนที่ 9 : การทำ Authentication in Azure Mobile Services ด้วย Android
Rating :

 
ตอนที่ 10 : การทำ Push Notifications in Mobile Services ด้วย Android
Rating :

 
ตอนที่ 11 : การทำ Validate และ Modify data in Mobile Services บน Andriod
Rating :

 
ตอนที่ 12 : การทำ Refine Mobile Services queries with paging บน Android
Rating :

 
ตอนที่ 13 : การเขียน Scripts to authorize users in Mobile Services บน Android
Rating :

 
ตอนที่ 14 : Show Case 1 : Register Form (Android and Mobile Services)
Rating :

 
ตอนที่ 16 : Show Case 3 : Update Data (Android and Mobile Services)
Rating :

 
ตอนที่ 17 : Show Case 4 : Delete Data (Android and Mobile Services)
Rating :

 
ตอนที่ 18 : บทความอื่น ๆ เกี่ยวกับ Android Mobile Services บน Windows Azure
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่