Android-based Bmob mobile backend ECs, androidbmob

Source: Internet
Author: User

Android-based Bmob mobile backend ECs, androidbmob

Source code download: http://download.csdn.net/download/jjhahage/10034519

PS: Generally, when writing an android program, you can use servlet as the server to filter unregistered users, which is too troublesome, it is not always usable. Here we will introduce a mobile backend ECs platform bmob, which not only achieves cloud database storage, but also obtains mobile phone verification and so on. It is easy anytime, anywhere. Here is a small demo, implement a login registration function to add, query, modify, and delete logs. The following is an example to implement the login registration function.

1: first go to the bmob official website and register an account to create a project,

2: Create an android Project (AndroidStudio)
(1): Add dependency: build in app. add compile 'cn. bmob. android: bmob-sdk: 3.4.6 'compile 'com. squareup. okhttp: 2.4.0 '// The CDN file service uploads and downloads files using okhttp-related packages (required) compile 'com. squareup. okio: 1.4.0 'sourceSets {main. jniLibs. srcDirs = ['libs']} useLibrary 'org. apache. http. legacy'

 

 

Location


(2) Add permissions:

<! -- Allow INTERNET connection --> <uses-permission android: name = "android. permission. INTERNET"/> <! -- Get network status information such as GSM (2g) and WCDMA (China Unicom 3g) --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/> <! -- Get wifi network status information --> <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/> <! -- Keep the CPU running. The screen and keyboard lights may be disabled for file upload and download --> <uses-permission android: name = "android. permission. WAKE_LOCK"/> <! -- Get the write permission of the SD card for file upload and download --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> <! -- Allow reading the mobile phone status to create BmobInstallation --> <uses-permission android: name = "android. permission. READ_PHONE_STATE"/>

 

 

 

(3): Add maven to the specified cloud database.

 

maven { url "https://raw.github.com/bmob/bmob-android-sdk/master"}

 

 

(4 :) initialization:

 

Bmob. initialize (this, "Your Application ID ");

3: The following is the code.

Write an object class person,

 

package cn.day1.model;import cn.bmob.v3.BmobObject;/** * Created by CMusketeer on 17/10/22. */public class Person extends BmobObject {    private String name;    private String password;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

 

Write three la S: registration page, logon page, and logon success page

 

Activity_main.xml

 

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout 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" android: orientation = "vertical" tools: context = "cn. day1.bmobtest1. mainActivity "> <TextView android: gravity =" center "android: textSize =" 20dp "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: text = "login"/> <EditText android: id = "@ + id/id_username" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "username"/> <EditText android: id = "@ + id/id_userpassword" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "password"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/id_ OK" android: layout_width = "0dp" android: text = "login" android: layout_height = "wrap_content" android: layout_weight = "1"/> <Button android: id = "@ + id/id_register" android: text = "register" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1"/> </LinearLayout>

Registration page: register_layout.xml. You have written all the pages first, and it will be easy to do later.

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout 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" android: orientation = "vertical" tools: context = "cn. day1.bmobtest1. mainActivity "> <TextView android: gravity =" center "android: textSize =" 20dp "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: text = "Registration Center"/> <EditText android: id = "@ + id/id_register_username" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "username"/> <EditText android: id = "@ + id/id_register_userpassword" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "password"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/id_register_ OK" android: text = "register" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1"/> </LinearLayout>

 

Logon success page: success. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: layout_width = "match_parent" android: layout_height = "match_parent" android: text = "successfully logged on" android: gravity = "center" android: textSize = "50dp"/> </LinearLayout>

Register Activity, RegisterActivity. java function: add

Here is a simple registration, with no judgment in it. Therefore, a single number can be registered repeatedly, but only a unique ID.

Package cn. day1.bmobtest1; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. textView; import android. widget. toast; import cn. bmob. v3.listener. saveListener; import cn. day1.model. person;/*** Created by CMusketeer on 17/10/22. */public class RegisterActivity extends Activity {private TextView register_user; private TextView Register_password; private Button register_ OK; private Person p2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. register_layout); addControl (); // load control addRegisterShow (); // registration method} private void addRegisterShow () {register_ OK .setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {final String RUser = register_user.getText (). toString (). trim (); String rPassword = register_password.getText (). toString (). trim (); // determines whether the user name and password are empty. If (rUser. length ()> 0 & rPassword. length ()> 0) {p2 = new Person (); p2.setName (rUser); p2.setPassword (rPassword); // The insertion method p2.save (RegisterActivity. this, new SaveListener () {@ Override public void onSuccess () {// TODO Auto-generated method stub register_password.setText (""); register_user.setText (""); Toast. makeText (RegisterActivity. this, "the data is successfully added, and the returned objectId is:" + p2.getObjectId (), Toast. LENGTH_SHORT ). show () ;}@ Override public void onFailure (int code, String msg) {// TODO Auto-generated method stub Toast. makeText (RegisterActivity. this, "failed to create data:" + msg, Toast. LENGTH_SHORT ). show () ;}}) ;}else {Toast. makeText (RegisterActivity. this, "the user name or password cannot be blank", Toast. LENGTH_SHORT ). show () ;}}) ;}private void addControl () {register_user = (TextView) findViewById (R. id. id_register_username); register_password = (TextView) findViewById (R. id. id_register_userpassword); register_ OK = (Button) findViewById (R. id. id_register_ OK );}}

 

Logon page: MainActivity. java function: Query

 

 

Package cn. day1.bmobtest1; import android. content. intent; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. util. log; import android. view. view; import android. widget. button; import android. widget. textView; import android. widget. toast; import java. util. list; import cn. bmob. v3.Bmob; import cn. bmob. v3.BmobQuery; import cn. bmob. v3.listener. findListener; import cn. day1.model. person; public class MainActivity extends AppCompatActivity {private Person p2; private TextView lgUser; private TextView lgPassword; private Button btn_ OK; private Button btn_rg; @ Override protected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); Bmob. initialize (this, "Your Application id"); setContentView (R. layout. activity_main); addControl (); addLogin ();} private void addLogin () {btn_rg.setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (MainActivity. this, RegisterActivity. class); startActivity (intent) ;}}); btn_ OK .setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {BmobQuery <Person> query = new BmobQuery <Person> (); query. findObjects (MainActivity. this, new FindListener <Person> () {String lgU = lgUser. getText (). toString (). trim (); String lgp = lgPassword. getText (). toString (). trim (); int panduan = 1; @ Override public void onSuccess (List <Person> list) {for (int I = 0; I <list. size (); I ++) {String name = list. get (I ). getName (); String password = list. get (I ). getPassword (); Log. e ("user", "Unique id:" + list. get (I ). getObjectId () + "----" + name + "---" + password); if (name. equals (lgU) & password. equals (lgp) {Toast. makeText (MainActivity. this, "Logon successful", Toast. LENGTH_SHORT ). show (); panduan = 2; // If panduan is equal to 2, the system jumps out of this loop and clears all input records to jump to the specified page lgUser. setText (""); lgPassword. setText (""); Intent intent = new Intent (MainActivity. this, Success. class); startActivity (intent); break;} if (panduan = 1) {Toast. makeText (MainActivity. this, "Logon Failed", Toast. LENGTH_SHORT ). show () ;}@ Override public void onError (int I, String s) {}}) ;}} private void addControl () {lgUser = (TextView) findViewById (R. id. id_username); lgPassword = (TextView) findViewById (R. id. id_userpassword); btn_ OK = (Button) findViewById (R. id. id_ OK); btn_rg = (Button) findViewById (R. id. id_register );}}

 


Success. java

 

 

package cn.day1.bmobtest1;import android.app.Activity;import android.os.Bundle;/** * Created by CMusketeer on 17/10/22. */public class Success extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.success);    }}

 

Summary:

 

The unique id can be obtained through the user name. When the user enters the user name, as long as the user name in the database is consistent with the input, the user can list. get (I). getObjectId ()

Process addition, query, modification, and addition: person = new Person (); person. setName (user); person. setAddress (password); person. save (new SaveListener <String> () {@ Override public void done (String s, BmobException e) {if (e = null) {Toast. makeText (MainActivity. this, "success", Toast. LENGTH_SHORT ). show () ;}else {}}); you can find all the IDs to obtain the idid = list. get (I ). getObjectId (); person = new Person (); person. delete (id, new UpdateListener () {@ Override public void done (BmobException e) {if (e = null) {Log. e ("sss", "deleted successfully") ;}}); query: This is not the same as the preceding query. This is also a method of // querying all queries. findObjects (new FindListener <Person> () {@ Override public void done (List <Person> list, BmobException e) {}} // query a single query. change getObject (id, new listener) to person. setName ("111"); person. update (id, new UpdateListener () {@ Override public void done (BmobException e) {if (e = null) {// Toast. makeText (MainActivity. this, "changed successfully", Toast. LENGTH_SHORT ). show (); Log. e ("sss", "changed successfully ");}}

 

:

 




 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.