How to insert data to Mobile Services - Android (Windows Azure)
How to insert data to Mobile Services - Android (Windows Azure) สรุปวิธีการบันทึก Insert ข้อมูลเข้า Table ของ Mobile Services บน Windows Azure ด้วย Android แบบสั้น ๆ ง่าย ๆ
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 final void setUsername(String username) {
mUsername = username;
}
public final void setPassword(String password) {
mPassword = password;
}
public final void setName(String name) {
mName = name;
}
public final void setTel(String tel) {
mTel = tel;
}
public final void setEmail(String email) {
mEmail = email;
}
}
MainActivity.java
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 Services ***/
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 EditText txtName = (EditText)findViewById(R.id.txtName);
final EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
final EditText txtTel = (EditText)findViewById(R.id.txtTel);
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
// btnSave
final Button btnSave = (Button) findViewById(R.id.btnSave);
// Perform action on click
btnSave.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
{
/*** Item for Insert ***/
MyMember item = new MyMember();
item.setUsername(txtUsername.getText().toString());
item.setPassword(txtPassword.getText().toString());
item.setName(txtName.getText().toString());
item.setTel(txtTel.getText().toString());
item.setEmail(txtEmail.getText().toString());
// Insert the new item
mMyMember.insert(item, new TableOperationCallback<MyMember>() {
public void onCompleted(MyMember entity, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
AlertDialog ad = adb.create();
ad.setMessage("Register Data Successfully.");
ad.show();
} else {
AlertDialog ad = adb.create();
ad.setMessage("Error : " + exception.getCause().getMessage());
ad.show();
}
}
});
}
}
});
}
Example
Show Case 1 : Register Form (Android and Mobile Services)