Bmob(云服务器)

Bmob的使用详见官网:www.bmob.cn

Bmob云服务简介

Bmob是一个移动后端云服务平台,提供了后端服务器的数据服务、文件服务、推送服务等,让开发者可以专注客户端的开发,从而达到快速构建一个APP的目的。

入门和BmobSDK集成

加依赖包
参考文档:http://doc.bmob.cn/data/android/

添加权限

<!--允许联网 --> 
<uses-permission android:name="android.permission.INTERNET" /> 
<!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  --> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<!--获取wifi网络状态的信息 --> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<!--获取sd卡写的权限,用于文件上传和下载-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许读取手机状态 用于创建BmobInstallation--> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

初始化BmobSDK
在你应用程序启动的Application的onCreate()方法中初始化Bmob功能。

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

增删改查

添加一行数据

package com.example.bmob;

import cn.bmob.v3.BmobObject;

public class Person extends BmobObject {
    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
public void addClick(View view){
        Person p=new Person();
        p.setName("杨哥");
        p.setAddress("温州");
        p.save(new SaveListener<String>(){
            @Override
            public void done(String s, BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this, "添加数据成功,返回objectId为:"+s, Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"创建数据失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

云服务器安卓存储空间不足 android云服务器_云服务器安卓存储空间不足


删除一行数据

public void delClick(View view){
        Person p=new Person();
        p.setObjectId("42cf195369");
        p.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

修改一行数据

public void updateClick(View view){
        Person p2 = new Person();
        p2.setAddress("温州苍南");
        p2.update("42cf195369", new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this,"更新成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"更新失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

云服务器安卓存储空间不足 android云服务器_android_02


获取一行数据

public void queryClick(View view){
        final BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
        bmobQuery.getObject("42cf195369", new QueryListener<Person>() {
            @Override
            public void done(Person object,BmobException e) {
                if(e==null){
                    Toast.makeText(MainActivity.this,"查询成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"查询失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

登录注册

User.class

package com.example.bmob;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;

public class User extends BmobUser {
    private BmobFile icon;

    public BmobFile getIcon() {
        return icon;
    }

    public void setIcon(BmobFile icon) {
        this.icon = icon;
    }
}

RegActivity

package com.example.bmob;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;


public class RegActivity extends AppCompatActivity {

    private EditText editText_name,editText_pass,editText_email,editText_icon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        Bmob.initialize(this,"b3b38273a48df0dbfb58ffc6aba66e2d");
        editText_name=findViewById(R.id.editText_username);
        editText_pass=findViewById(R.id.editText_password);
        editText_email=findViewById(R.id.editText_email);
//        editText_icon=findViewById(R.id.editText_icon);
    }

    public void registerClick(View view){
        String name=editText_name.getText().toString();
        String pass=editText_pass.getText().toString();
        String email=editText_email.getText().toString();
//        String icon=editText_icon.getText().toString();

        User user=new User();
        user.setUsername(name);
        user.setPassword(pass);
        user.setEmail(email);
//        user.setIcon(icon);
        user.signUp(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
                    Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(RegActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

LoginActivity

package com.example.bmob;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity {
    private EditText editText_username, editText_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Bmob.initialize(this, "b3b38273a48df0dbfb58ffc6aba66e2d");
        editText_username = (EditText) findViewById(R.id.editText_username);
        editText_password = (EditText) findViewById(R.id.editText_password);
    }

    public void loginClick(View view){
        String username = editText_username.getText().toString();
        String password = editText_password.getText().toString();
        final BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername(username);
        bmobUser.setPassword(password);
        bmobUser.login(new SaveListener<User>() {
            @Override
            public void done(User bmobUser, BmobException e) {
                if (e == null) {
                    User user = BmobUser.getCurrentUser(User.class);
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("user", user);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "登录失败,用户名或密码不正确", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    public void registerClick(View view){
        Intent intent = new Intent(this, RegActivity.class);
        startActivity(intent);
    }
}

云服务器安卓存储空间不足 android云服务器_ico_03

云服务器安卓存储空间不足 android云服务器_android_04

便签案例

云服务器安卓存储空间不足 android云服务器_ico_05

云服务器安卓存储空间不足 android云服务器_云服务器安卓存储空间不足_06

云服务器安卓存储空间不足 android云服务器_ico_07


代码如下

Note.class

package com.example.bmob;

import cn.bmob.v3.BmobObject;

public class Note extends BmobObject {
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

NoteListActivity

package com.example.bmob;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

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.UpdateListener;

public class NoteListActivity extends AppCompatActivity {
    private static final int DEL_ITEM =0x1 ;
    private ArrayList<Note> notes=new ArrayList<>();
    private ListView listView;
    NoteAdapter na;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_list);
        Bmob.initialize(this,"b3b38273a48df0dbfb58ffc6aba66e2d");
        listView = (ListView) findViewById(R.id.listView_note);
        loadData();
//        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,notes);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView_content= view.findViewById(R.id.textView5_content);
                String content=textView_content.getText().toString();
                String objectid= (String) view.getTag();
                Intent intent=new Intent(NoteListActivity.this,NoteDetailActivity.class);
                intent.putExtra("content",content);
                intent.putExtra("objectid",objectid);
                startActivity(intent);
            }
        });
        registerForContextMenu(listView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        BmobQuery<Note> query=new BmobQuery<>();
        query.setLimit(50);//限定查询条数
        query.findObjects(new FindListener<Note>() {
            @Override
            public void done(List<Note> list, BmobException e) {
                if (e==null){
                    notes= (ArrayList<Note>) list;
                    na=new NoteAdapter(NoteListActivity.this,notes);
                    listView.setAdapter(na);
                }
            }
        });
    }

    static class NoteAdapter extends BaseAdapter {
        private Context context;
        private List<Note> list;
        public NoteAdapter(Context context, List<Note> list){
            this.context=context;
            this.list=list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                convertView= LayoutInflater.from(context).inflate(R.layout.layout_note_item,null);
            }
            Note note=list.get(position);
            TextView content=convertView.findViewById(R.id.textView5_content);
            content.setText(note.getContent());
            convertView.setTag(note.getObjectId());
            return convertView;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_note_list,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_note:
                Intent intent=new Intent(this,NoteNewActivity.class);
                startActivity(intent);
                break;
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1,DEL_ITEM,100,"删除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case DEL_ITEM:
                AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                View view= info.targetView;
                String objectid= (String) view.getTag();
                Note note=new Note();
                note.setObjectId(objectid);
                note.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            loadData();
                        }
                    }
                });
                break;
        }
        return super.onContextItemSelected(item);
    }
}

NoteDetailActivity

package com.example.bmob;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.EditText;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class NoteDetailActivity extends AppCompatActivity {
    private EditText editText_content;
    private String objectid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_detail);
        editText_content=findViewById(R.id.editText5_content);
        objectid=getIntent().getStringExtra("objectid");
        editText_content.setText(getIntent().getStringExtra("content"));
    }

    public void update(View view){
        String content=editText_content.getText().toString();
        if (!TextUtils.isEmpty(content)){
            Note note=new Note();
            note.setContent(content);
            note.update(objectid, new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if(e==null){
                        Toast.makeText(NoteDetailActivity.this, "修改便签成功", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(NoteDetailActivity.this,NoteListActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(NoteDetailActivity.this,"修改便签失败",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}

NoteNewActivity

package com.example.bmob;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.EditText;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class NoteNewActivity extends AppCompatActivity {
    private EditText editText_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_new);
        editText_content=findViewById(R.id.editText_content);
    }

    public void save(View view){
        String content=editText_content.getText().toString();
        if (!TextUtils.isEmpty(content)){
            Note note=new Note();
            note.setContent(content);
            note.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if(e==null){
                        Toast.makeText(NoteNewActivity.this, "保存便签成功", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(NoteNewActivity.this,NoteListActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(NoteNewActivity.this,"保存便签失败",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}

activity_note_detail

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoteDetailActivity">
    <EditText
        android:id="@+id/editText5_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/update"
        android:onClick="update"
        android:text="保存"
        android:layout_below="@id/editText5_content"/>
</RelativeLayout>

activity_note_list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoteListActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView_note"
        >
    </ListView>
</RelativeLayout>

activity_note_new

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoteNewActivity">
    <EditText
        android:id="@+id/editText_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/save"
        android:onClick="save"
        android:text="保存"
        android:layout_below="@id/editText_content"/>
</RelativeLayout>

layout_note_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView5_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

menu_note_list

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.bmob1.NoteListActivity"
    >
    <item android:id="@+id/add_note" android:title="添加"
        android:orderInCategory="100" app:showAsAction="always"/>

</menu>