移動後端雲平台Bmob介紹,後端雲平台bmob
對於移動端的獨立開發人員來說,最痛苦的事情莫過於搭建後台伺服器。沒有基礎的還得從頭學起,有技術的又要搭建維護後台,非常麻煩。而移動後端雲平台的出現,簡直是每個獨立開發人員的福音,它可以免費提供伺服器,有資料檔案服務,還有社交功能等。國內主流的有友盟、極光推送,Bmob,AVOS Cloud等,下面介紹老少鹹宜的Bmob
如何連結到Bmob的伺服器開發文檔都有,開發文檔有沒具體介紹的是檔案下載功能。檔案下載,首先是通過資料查詢獲得
BmobFile對象 ,然後通過該對象的getFileUrl()方法獲得下載連結,最後通過連結下載。下面是一個實現了添加資料、查詢資料、檔案上傳、檔案下載、推送功能的例子:
package com.example.bombexample;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.json.JSONObject;import com.lidroid.xutils.HttpUtils;import com.lidroid.xutils.exception.HttpException;import com.lidroid.xutils.http.ResponseInfo;import com.lidroid.xutils.http.callback.RequestCallBack;import cn.bmob.push.BmobPush;import cn.bmob.v3.Bmob;import cn.bmob.v3.BmobInstallation;import cn.bmob.v3.BmobPushManager;import cn.bmob.v3.BmobQuery;import cn.bmob.v3.datatype.BmobFile;import cn.bmob.v3.listener.FindListener;import cn.bmob.v3.listener.GetCallback;import cn.bmob.v3.listener.GetListener;import cn.bmob.v3.listener.SaveListener;import cn.bmob.v3.listener.UploadFileListener;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {private List<User> users=new ArrayList<User>();private HttpUtils httpUtils=null;BmobFile bmobFile =null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Bmob.initialize(this, "3b728750d1755e56dd680fa76c4a8386"); // 使用推送服務時的初始化操作 BmobInstallation.getCurrentInstallation(this).save(); // 啟動推送服務 BmobPush.startWork(this, "3b728750d1755e56dd680fa76c4a8386"); httpUtils=new HttpUtils();}// 向服務期添加資料public void add(View view){ for(int i=0;i<10;i++){User user=new User();user.setName("gaigai"+i);user.setPassword("hlb"+i);users.add(user);}for(final User user:users){user.save(this, new SaveListener() {@Overridepublic void onSuccess() {Toast.makeText(MainActivity.this, "添加資料成功"+user.getObjectId(), Toast.LENGTH_SHORT).show();}@Overridepublic void onFailure(int arg0, String arg1) {Toast.makeText(MainActivity.this, "添加資料失敗", Toast.LENGTH_SHORT).show();}});}}// 查詢服務器資料public void query(View view){BmobQuery<User> query=new BmobQuery<User>(); query.getObject(this,"f69efc2ca8",new GetListener<User>() {@Overridepublic void onSuccess(User arg0) {Toast.makeText(MainActivity.this,arg0.getName()+" "+arg0.getPassword(), Toast.LENGTH_SHORT).show();}@Overridepublic void onFailure(int arg0, String arg1) {}});}// 查詢服務器所有資料public void queryAll(View view){BmobQuery<User> query=new BmobQuery<User>();query.findObjects(this, new FindListener<User>() {@Overridepublic void onSuccess(List<User> arg0) {Toast.makeText(MainActivity.this, arg0.size()+"", Toast.LENGTH_SHORT).show();}@Overridepublic void onError(int arg0, String arg1) {// TODO Auto-generated method stub}});}// 推送功能public void data_push(View view){BmobPushManager bmobPush = new BmobPushManager(this);bmobPush.pushMessageAll("哈哈哈哈.");}// 檔案上傳public void upload(View v) throws IOException{InputStream in=getAssets().open("byx.jpg");File file=new File(Environment.getExternalStorageDirectory()+"/byx.jpg");if(!file.exists()){file.createNewFile();}FileOutputStream out=new FileOutputStream(file);int len=0;byte []buffer=new byte[1024];while((len=in.read(buffer))!=-1){out.write(buffer, 0, len);}out.close();in.close();bmobFile = new BmobFile(file);bmobFile.upload(this, new UploadFileListener() {@Overridepublic void onSuccess() {// TODO Auto-generated method stubToast.makeText(MainActivity.this, bmobFile.getFileUrl(MainActivity.this)+"", Toast.LENGTH_SHORT).show();Log.i("Url", bmobFile.getFileUrl(MainActivity.this));}@Overridepublic void onFailure(int arg0, String arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "上傳檔案失敗", Toast.LENGTH_SHORT).show();}});}// 檔案下載public void download(View v){BmobQuery<Test> query=new BmobQuery<Test>();query.getObject(this, "nt7rAAAG", new GetListener<Test>() {@Overridepublic void onSuccess(Test arg0) {arg0.getThumbnail().getFileUrl(MainActivity.this);Log.i("url",arg0.getThumbnail().getFileUrl(MainActivity.this));downloadPic(arg0.getThumbnail().getFileUrl(MainActivity.this),arg0.getThumbnail().getFilename());}@Overridepublic void onFailure(int arg0, String arg1) {// TODO Auto-generated method stub}});}public void downloadPic(String url,String filename){httpUtils.download(url, Environment.getExternalStorageDirectory()+"/"+filename, new RequestCallBack<File>() {@Overridepublic void onSuccess(ResponseInfo<File> responseInfo) {Log.i("download","檔案儲存體路徑"+responseInfo.result.getPath());}@Overridepublic void onFailure(HttpException error, String msg) {// TODO Auto-generated method stub}});}}
項目:http://download.csdn.net/detail/aehaojiu/8653135