Introduction to mobile backend cloud platform Bmob, backend cloud platform bmob

Source: Internet
Author: User

Introduction to mobile backend cloud platform Bmob, backend cloud platform bmob
For independent developers on mobile terminals, the most painful thing is to build backend servers. If you have no foundation, you have to start from scratch. If you have technology, you have to build and maintain the background, which is very troublesome. The emergence of the mobile backend cloud platform is a blessing for every independent developer. It provides servers, data file services, and social networking functions for free. Mainstream domestic platforms include umeng, Aurora push, Bmob, and AVOS Cloud. The following describes the Bmob suitable for old and young people.

The server development documentation on how to link to Bmob is available. The development documentation does not specifically introduce the file download function. File Download, first obtained through Data Query

BmobFile object, and then get the download link through the getFileUrl () method of the object, and finally download through the link. The following is an example of how to add, query, upload, download, and push data:

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 savedIns TanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Bmob. initialize (this, "3b728750d1755e56dd680fa76c4a8386"); // use the initialization operation BmobInstallation when pushing the service. getCurrentInstallation (this ). save (); // start the PUSH Service BmobPush. startWork (this, "3b728750d1755e56dd680fa76c4a8386"); httpUtils = new HttpUtils ();} // add public void add (View view) {for (int I = 0; I <10; I ++) {User user = new User (); us Er. 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, "data added successfully" + user. getObjectId (), Toast. LENGTH_SHORT ). show () ;}@ Overridepublic void onFailure (int arg0, String arg1) {Toast. makeText (MainActivity. this, "failed to add data", Toast. LENGTH_SHORT ). show () ;}}}}// query server data publ Ic 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) {}}) ;}// query all server data public void queryAll (View view) {BmobQuery <User> query = ne W 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 }});} // push function public void data_push (View view) {BmobPushManager bmobPush = new BmobPushManager (this); bmobPush. pushMessa GeAll ("Hahahaha. ");} // File upload 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, "failed to upload file", Toast. LENGTH_SHORT ). show () ;}}) ;}// File download 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", "file storage path" + responseInfo. result. getPath () ;}@ Overridepublic void onFailure (HttpException error, String msg) {// TODO Auto-generated method stub }});}}

Project: http://download.csdn.net/detail/aehaojiu/8653135


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.