android 简单方法连接服务器-------云服务Bmob

来源:互联网 发布:医疗器械注册软件研究 编辑:程序博客网 时间:2024/06/03 10:52

首先Bmob是一个在线的云端服务器,安卓app可以向这个云服务器上传下载查询数据,使用起来比较简单方便

首先

第一步:需要在Bmob官网上面注册一个账号,然后登陆这个账号

第二步   :需要下载Bmob的SDK包,然后将其导入自己的libs中,这样就可以调用Bmob的类了

第三步:需要在Bmob中创建自己的应用,创建之后就会有自己的应用密匙,下图的Application ID。

第四步:在activity 中绑定应用密匙,可以参照Bomb官方的的简单教程



下面是建立的简单的基于

name和passwd的保存和查询

对于在Bmob中

对于一个下列的类就是一张表单

相当于下列的类是一个有两列分别为 name和passwd的表,每插入一个类的实例就会多一行数据

这个类必须继承BmobObject  

import android.graphics.Bitmap;import cn.bmob.v3.BmobObject;public class SavaData extends BmobObject{private String name;private String passwd;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPasswd() {return passwd;}public void setPasswd(String passwd) {this.passwd = passwd;}}


以下代码是对表的建立和查询

import java.util.List;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import cn.bmob.v3.Bmob;import cn.bmob.v3.BmobQuery;import cn.bmob.v3.listener.FindListener;import cn.bmob.v3.listener.SaveListener;import com.example.testbmob.R;public class TestBmob extends Activity{private TextView mName;private TextView mPasswd;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);Bmob.initialize(TestBmob.this, "这里填写 在Bmob中获取的app Id");//绑定 BmobmName=(TextView) findViewById(R.id.name);mPasswd=(TextView) findViewById(R.id.passwd);}public void query(View view){  //这是对于query的监听BmobQuery<SavaData> query= new BmobQuery<SavaData>();//query.addWhereEqualTo("name", "");  //按名字查询单个数据query.findObjects(TestBmob.this, new FindListener<SavaData>() {@Overridepublic void onSuccess(List<SavaData> Bmob) {// TODO Auto-generated method stubAlertDialog.Builder Dialog = new AlertDialog.Builder(TestBmob.this);Dialog.setTitle("query");String s="";for(SavaData save : Bmob){s+=save.getName()+"  "+save.getPasswd()+"\n";}Dialog.setMessage(s);Dialog.create().show();}@Overridepublic void onError(int arg0, String arg1) {// TODO Auto-generated method stubToast.makeText(TestBmob.this, "Error", Toast.LENGTH_LONG).show();}});}public void submit(View view){  //这里是对于 submit按钮的监听String name=mName.getText().toString();String passwd=mPasswd.getText().toString();SavaData data=new SavaData();data.setName(name);data.setPasswd(passwd);//保存 一个 Bmob的对象,第一次执行会建立表,后面执行是插入data.save(TestBmob.this,new SaveListener() {@Overridepublic void onSuccess() {// TODO Auto-generated method stubToast.makeText(TestBmob.this, "Success", Toast.LENGTH_LONG).show();}@Overridepublic void onFailure(int arg0, String arg1) {// TODO Auto-generated method stubToast.makeText(TestBmob.this, "Fail", Toast.LENGTH_LONG).show();}});}}

下面是一个很简单的一个界面,用来显示结果:
<?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"    android:orientation="vertical"         >    <EditText        android:id="@+id/name"        android:layout_width="fill_parent"        android:layout_height="wrap_content"       android:textSize="40sp"        android:hint="name" />    <EditText             android:id="@+id/passwd"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textSize="40sp"        android:hint="passwd" />    <Button              android:id="@+id/Submit"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textSize="40sp"        android:onClick="submit"        android:text="submit" />    <Button        android:id="@+id/Query"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="query"        android:text="query" /></LinearLayout>



0 0
原创粉丝点击