视频地址:http://www.imooc.com/learn/254
这次学习的视频里面主要是粗浅的讲了一下移动后端服务与其提供商,然后用Bmob后端云提供的相关的SDK进行了一下简单的测试,建议还是去认真的观看一下视频,因为一些细节用文字叙述起来起来比较麻烦,当然,也可以去Bmob的官网查相关的开发文档和开发教程以及Demo。
当然,下面有一个测试的Demo,就是简单将数据传输到云端的服务器上。
在编写代码之前,需要将Bmob提供的SDK导入到项目中,并且添加相应的权限(这些都可以在官网上有提供)
首先是一个简单的布局:
<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="com.example.bmob_test.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:id="@+id/id_et_name"
android:hint="Your name" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="submit" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id_et_feedback"
android:hint="Feedback"
android:lines="2" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Query All"
android:onClick="query"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="query for name"
android:id="@+id/id_et_query_one" />
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Query By Name"
android:onClick="queryOne" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Push All"
android:onClick="pushAll"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
这里的布局主要是为了实现向服务器添加自定义的数据,查询所有数据,以及根据某个字段查询指定的数据这三个功能。
然后是逻辑的实现:
public class MainActivity extends AppCompatActivity {
private EditText mName,mFeedback,mQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bmob.initialize(this,在Bmob注册应用所提供的Application ID);//初始化基础的SDK
mName= (EditText) findViewById(R.id.id_et_name);
mFeedback= (EditText) findViewById(R.id.id_et_feedback);
mQuery= (EditText) findViewById(R.id.id_et_query_one);
}
public void submit(View view) {
if(!isNetworkAvailable(MainActivity.this)) {
Log.d("测试","无网络");
Toast.makeText(MainActivity.this, "当前网络不可用,无法提交!", Toast.LENGTH_SHORT).show();
return;
}
String name=mName.getText().toString();
String feedback=mFeedback.getText().toString();
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(feedback)) {
Feedback fb=new Feedback();
fb.setName(name.trim());
fb.setFeedback(feedback.trim());
//将数据提交给服务器
fb.save(MainActivity.this, new SaveListener() {
@Override
public void onSuccess() {
Log.d("测试","提交成功");
Toast.makeText(MainActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int i, String s) {
Log.d("测试","提交失败");
Toast.makeText(MainActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
}
});
}
else {
Toast.makeText(MainActivity.this, "姓名或者内容不能为空!", Toast.LENGTH_SHORT).show();
}
}
public void query(View view) {
BmobQuery<Feedback> query=new BmobQuery<>();
query.findObjects(MainActivity.this, new FindListener<Feedback>() {
@Override
public void onSuccess(List<Feedback> list) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Query");
String str="";
for(Feedback fb:list)
str += fb.toString();
builder.setMessage(str);
builder.create().show();
}
@Override
public void onError(int i, String s) {
Log.d("测试","查询失败");
}
});
}
public void queryOne(View view) {
String name=mQuery.getText().toString();
if(!TextUtils.isEmpty(name)) {
BmobQuery<Feedback> query=new BmobQuery<>();
query.addWhereEqualTo("name",name.trim());//增加查询的条件
query.findObjects(MainActivity.this, new FindListener<Feedback>() {
@Override
public void onSuccess(List<Feedback> list) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Query");
String str="";
for(Feedback fb:list)
str += fb.toString();
builder.setMessage(str);
builder.create().show();
}
@Override
public void onError(int i, String s) {
Log.d("测试","查询失败");
}
});
}
else {
Toast.makeText(MainActivity.this, "姓名不能为空!", Toast.LENGTH_SHORT).show();
}
}
/**
* 判断网络是否可用
*/
public boolean isNetworkAvailable(Activity activity) {
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null)
{
return false;
}
else
{
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (int i = 0; i < networkInfo.length; i++) {
Log.d("测试",i + "===状态===" + networkInfo[i].getState());
Log.d("测试",i + "===类型===" + networkInfo[i].getTypeName());
// 判断当前网络状态是否为连接状态
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
需要注意的是:发送到服务器的数据是以对象的形式,所以需要建立相应的bean类,并且该类还需要继承BmobObject类,每一个实例都对应于数据库中相应的表的一行数据。
当提交需要的数据时,如果对应数据的表还没有建立,云端的服务器会自动根据bean类的类名与成员变量建立对应的表。
因此,在这里还需要一个Feedback的数据实体类,里面包含两个成员变量,也就是数据对应的字段,以及相应的Getter与Setter
String name;
String feedback;
在测试的时候,如果正常提交了数据,就会在Bmob相应应用的控制台中看到对应的数据了。然后查询按钮,也会显示服务器上添加的数据。