Parse SDK使用记录

Parse是BaaS(Backend as a Service)的先驱, 它可以对多终端的资源进行管理, 发布通知等. 大大简化了后端的开发难度. 废话不多讲, 用过才知道,莽夫就是干!

Server 服务端

安装Parse Server

Parse Server需要Node.js与MongoDb, 安装方法不赘述, 到处都是
要是实在懒可以尝试以下方法

$ sh <(curl -fsSL https://raw.githubusercontent.com/parse-community/parse-server/master/bootstrap.sh)
$ npm install -g mongodb-runner
$ mongodb-runner start
$ npm start

真香! 还是一键脚本舒服… 按照提示输入一些配置信息
出现下图表示server安装成功
Jietu20181208-001833@2x.jpg

Dashboard 仪表盘

在Server安装完成后, 所有的功能都有了, 但是不能可视化的看到后台的数据, 所以更Geek的做法是安装一个Dashboard
npm install -g parse-dashboard
安装完成之后, 命令号执行
parse-dashboard --appId yourAppId --masterKey yourMasterKey --serverURL "https://example.com/parse" --appName optionalName
这里的配置信息可以在Server的安装目录下的config.json文件找到, 将其更改到你的配置.
运行之后打开http://0.0.0.0:4040/apps
如果看到下图, 说明你Dashboard又安装成功了.
Jietu20181208-003006@2x.jpg

Client 客户端

Client是指端设备 Android/iOS/Web. 因为Parse提供API接口, 同时也提供各个平台的SDK, 使得Parse可以成为许多平台的后端. 这里主要以Android为例. (iOS我也不会)

安装依赖

添加软件源

首先在build.gradle(Project: XXX)中添加

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

这里是为maven仓库添加一个新的软件源
[scode type=“lblue”]Easy to use package repository for Git. Publish your JVM and Android libraries[/scode]
摘自jitpack.io 大体意思是说jitpack是个JVM/Android包仓库, 很多github/gitee等社区的库都会被发布在这里.

添加依赖项

其次, 在build.gradle(Module:app)中添加

dependencies {
    implementation "com.github.parse-community.Parse-SDK-Android:parse:latest.version.here"
}

上述代码摘自Github官方README.md 有个注意的是 代码有部分是placeholder latest.version.here 目的是让你使用最新版, 目前最新版是1.18.5, 把latest.version.here替换成1.18.5, 然后Sync, Sync成功后, 依赖就安装完成了.

如何使用

创建Application

首先, 要创建一个Application, 此Application非彼Application, 不是APP. Application/Service/Activity同为系统组件. 等等也许你又问了:

[scode type=“yellow”]Activity我知道, Application是个啥?[/scode]

[scode type=“lblue”]Application和Activity,Service一样,是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象,用来存储系统的一些信息。通常我们是不需要指定一个Application的,这时系统会自动帮我们创建,如果需要创建自己 的Application,也很简单创建一个类继承 Application并在manifest的application标签中进行注册(只需要给Application标签增加个name属性把自己的 Application的名字定入即可)。
android系统会为每个程序运行时创建一个Application类的对象且仅创建一个,所以Application可以说是单例 (singleton)模式的一个类.且application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期。因为它是全局 的单例的,所以在不同的Activity,Service中获得的对象都是同一个对象。所以通过Application来进行一些,数据传递,数据共享 等,数据缓存等操作。
[/scode]

在项目新建一个类, 然后继承自Application
具体代码如下, 如何新建一个Class, 我不想说!!!

public class ApplicationStarter extends Application{
    @Override
    public void onCreate() {
        super.onCreate();

        Parse.enableLocalDatastore(this);
        Parse.initialize(new Parse.Configuration.Builder(this)
                .applicationId("pZJJ7iSBoWNcN6sFYFvaFHSVX4RhBpKtk6E1AZti")
                .clientKey("X5bmnE9IYdVnWd1Ps7Tn7p6sbEtIIP213JnT3tFx")
                .server("http://10.0.2.2:1337/parse/") // for debug only! localhost
                .build()
        );

        ParseUser.enableAutomaticUser();

        ParseACL acl = new ParseACL();
        acl.setPublicReadAccess(true);
        acl.setPublicWriteAccess(true);
        ParseACL.setDefaultACL(acl, true);
    }
}

如果使用安卓模拟器, 主机的IP为10.0.2.2:1337, 远端服务器就为公网IP

注册Application

由于我们手工创建, Android Studio没有自动为我们注册. 所以我们要去Manifest文件里注册这个Application
在Manifest文件中, 为application标签添加android:name属性, 值为.ApplicationStarter
修改完成后, 部分如下

    <application
        android:name=".ApplicationStarter"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
测试一下!

在刚刚的ApplicationonCreate方法中,添加如下代码

        ParseObject object = new ParseObject("ExampleObject");
        object.put("name","zhu bing");
        object.put("age","20");

        object.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if(e == null){
                    Log.i("Parse","save successfully");
                }else{
                    Log.i("Parse", "save unsuccessfully! " + e.toString());
                }
            }
        });

然后运行, 编译一定会通过的. 然后去Dashboard看一下是不是出现了新的record
Jietu20181208-004241@2x.jpg
如果是, 那么恭喜! Parse已经可以用了.
[scode type=“yellow”]注意我这里的服务器地址配置, 我是在模拟器上运行, 如果你不是, 那就改掉它[/scode]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Develop the backend of your applications instantly using Parse iOS SDK Overview Build your applications using Parse iOS which serves as a complete cloud-based backend service Understand and write your code on cloud to minimize the load on the client side Learn how to create your own applications using Parse SDK, with the help of the step- by- step, practical tutorials In Detail Parse using iOS SDK is a new technology, and is the first of its kind in the field of mobile application development. It provides you the cloud where you can keep your data, host your code, and even your website without any hassle. It provides SDK so that you can access your data through your mobile and web applications. This practical, hands- on guide will help you to instantly get started with Parse iOS. It is packed with step- by- step exercises, which will help you to take advantage of the real power of the Parse iOS cloud backend service, and provides you with an example- based approach to help you build applications using Parse iOS. Starting with Parse iOS installation, we will move onto integration, and finally, this guide will end with the development of an application using Parse iOS. You will also learn about securing your application data by specifying ACL and Roles to your data objects. We will also learn about configuration in detail, and the implementation of cloud code to make your application lighter on the client side. You can take advantage of iCloud by hosting your website as well. You will learn everything that you need to know to develop your application using Parse iOS as a backend. What you will learn from this book Learn how to handle objects and queries in Parse Work with subclasses and files, to use the Parse iOS cloud to save data Discover how to configure your application on Parse along with its installation in your project Use Parse analytics for tracking users' activity on applications Understand user creation with login process and user roles to secure data on Parse iOS cloud Get to grips with the different ways to handle errors along with securing your data on cloud Approach A practical guide, featuring step-by-step instructions showing you how to use Parse iOS, and handle your data on cloud. Who this book is written for If you are a developer who wants to build your applications instantly using Parse iOS as a back end application development, this book is ideal for you. This book will help you to understand Parse, featuring examples to help you get familiar with the concepts of Parse iOS. Product Details Paperback: 112 pages Publisher: Packt Publishing (October 22, 2013) Language: English ISBN-10: 1783550333 ISBN-13: 978-1783550333 Product Dimensions: 9.2 x 7.5 x 0.2 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值