安卓Bmob資料新增和查詢以及驗證碼
阿新 • • 發佈:2018-11-28
記錄一下利用Bmob對資料的增加和查詢以及驗證碼的索取,記錄以便以後的使用
原始碼點這裡,該demo實現了驗證簡訊以及資料上傳和圖片上傳
1,對於Bmob的基本配置建議百度,別人介紹的比我要好。
2,新建一個Lost類
package com.example.bmob;
import cn.bmob.v3.BmobObject;
/**
* Created by 17478 on 2018/4/6.
*/
public class Lost extends BmobObject{
private String title;//標題
private String describe;//描述
private String phone;//聯絡手機
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3,對activity_main佈局檔案進行編寫
<?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:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bmob.MainActivity">
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="標題"
/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="手機"
/>
<EditText
android:id="@+id/describe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="描述"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_gravity="center"
android:text="新增"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_gravity="center"
android:text="查詢"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_gravity="center"
android:text="傳送驗證"/>
</LinearLayout>
4,新建一個Second類,用於顯示查詢到的資料,採用ListView
package com.example.bmob;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
public class Second extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final BmobQuery<Lost> bmobQuery = new BmobQuery<Lost>();
//按照時間降序
bmobQuery.order("-createdAt");
bmobQuery.findObjects(new FindListener<Lost>(){
@Override
public void done(List<Lost> list, BmobException e) {
final String[] title = new String[list.size()];
final String[] describe = new String[list.size()];
final String[] phone = new String[list.size()];
if(e==null){
for(int i = 0;i<list.size();i++){
/*title[i] = list.get(i).getTitle();
describe[i] = list.get(i).getDescribe();
phone[i] = list.get(i).getPhone();*/
title[i] = list.get(i).getTitle();
title[i] += list.get(i).getDescribe();
title[i] +=list.get(i).getPhone();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
Second.this,android.R.layout.simple_list_item_1,title);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}else{
Toast.makeText(Second.this, "查詢失敗", Toast.LENGTH_SHORT).show();
}
}
});
}
}
5,MainActivity:
package com.example.bmob;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import cn.bmob.push.BmobPush;
import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText editText1;
private EditText editText2;
private EditText editText3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//這裡的123換成你自己的application id
Bmob.initialize(this,"123");
BmobSMS.initialize(this,"123");
/* Person p2 = new Person();
p2.setName("mkc");
p2.setAddress("上海");
p2.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if(e==null){
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "失敗", Toast.LENGTH_SHORT).show();
}
}
});*/
editText1 = (EditText)findViewById(R.id.title);
editText2 = (EditText) findViewById(R.id.phone);
editText3 = (EditText)findViewById(R.id.describe);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
String inputtitle = editText1.getText().toString();
String inputphone = editText2.getText().toString();
String inputdescribe = editText3.getText().toString();
Lost lost = new Lost();
lost.setDescribe(inputdescribe);
lost.setPhone(inputphone);
lost.setTitle(inputtitle);
lost.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if(e==null){
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "失敗", Toast.LENGTH_SHORT).show();
}
}
});
break;
case R.id.button2:
Intent intent = new Intent(MainActivity.this,Second.class);
startActivity(intent);
case R.id.button3:
String number = editText2.getText().toString();
BmobSMS.requestSMSCode(this, number, "您的驗證碼是%smscode%,有效期為%ttl%分鐘。您正在使用%appname%驗證碼", new RequestSMSCodeListener(){
@Override
public void done(Integer integer, cn.bmob.sms.exception.BmobException e) {
if(e==null){
Toast.makeText(MainActivity.this, "驗證碼傳送成功,請儘快使用", Toast.LENGTH_SHORT).show();
}
}
});
default:
break;
}
}
}
6,activity_second:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bmob.Second">
<ListView
android:id="@+id/list_view"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
Over!