对移动真个独立开发者来讲,最痛苦的事情莫过于搭建后台服务器。没有基础的还得从头学起,有技术的又要搭建保护后台,非常麻烦。而移动后端云平台的出现,简直是每一个独立开发者的福音,它可以避免费提供论文服务器,有数据文件服务,还有社交功能等。国内主流的有友盟、极光推送,Bmob,AVOS Cloud等,下面介绍老少咸宜的Bmob
如何链接到Bmob的服务器开发文档都有,开发文档有没具体介绍的是文件下载功能。文件下载,首先是通过数据查询取得
BmobFile对象 ,然后通过该对象的getFileUrl()方法取得下载链接,最后通过链接下载。下面是1个实现了添加数据、查询数据、文件上传、文件下载、推送功能的例子:
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;
@Override
protected 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() {
@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "添加数据成功"+user.getObjectId(), Toast.LENGTH_SHORT).show();
}
@Override
public 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>() {
@Override
public void onSuccess(User arg0) {
Toast.makeText(MainActivity.this,
arg0.getName()+" "+arg0.getPassword(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int arg0, String arg1) {
}
});
}
// 查询
服务器所有数据
public void queryAll(View view)
{
BmobQuery<User> query=new BmobQuery<User>();
query.findObjects(this, new FindListener<User>() {
@Override
public void onSuccess(List<User> arg0) {
Toast.makeText(MainActivity.this, arg0.size()+"", Toast.LENGTH_SHORT).show();
}
@Override
public 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))!=⑴)
{
out.write(buffer, 0, len);
}
out.close();
in.close();
bmobFile = new BmobFile(file);
bmobFile.upload(this, new UploadFileListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, bmobFile.getFileUrl(MainActivity.this)+"", Toast.LENGTH_SHORT).show();
Log.i("Url", bmobFile.getFileUrl(MainActivity.this));
}
@Override
public void onFailure(int arg0, String arg1) {
// TODO Auto-generated method stub
Toast.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>() {
@Override
public 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());
}
@Override
public 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>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
Log.i("download","文件存储路径"+responseInfo.result.getPath());
}
@Override
public void onFailure(HttpException error, String msg) {
// TODO Auto-generated method stub
}
});
}
}
项目下载地址:http://download.csdn.net/detail/aehaojiu/8653135