zoukankan      html  css  js  c++  java
  • iOS开发中使用Bmob RESTful API

    简介

    尽管Bmob已经提供了一套SDK供开发者使用,但有时候开发者可能希望能直接与Bmob后台进行直接交互,以达到某些特别的需求(直接操作_User表、同步网络请求等)。而RESTful API可以使得只要能够发送HTTP请求的设备可以先Bmob进行数据交互。因此,在使用Bmob开发iOS应用过程中,我们也可以使用RESTful来完成交互。

    使用方法

    只要使用标准的HTTP请求即可。以添加对象为例,官方文档文档的描述如下:

    也就是我们需要设置以下几个参数
    - url
    - method
    - header
    - body

    代码如下:

         //设置URL
        NSURL *url = [NSURL URLWithString:@"https://api.bmob.cn/1/classes/Post"];
        
        //设置请求方法
        NSMutableURLRequest *addRequest = [[NSMutableURLRequest alloc] initWithURL:url];
        [addRequest setHTTPMethod:@"POST"];
        
        //设置请求头
        [addRequest setAllHTTPHeaderFields:@{@"X-Bmob-Application-Id":APPKEY,@"X-Bmob-REST-API-Key":RESTFULKEY,@"Content-Type":@"application/json"}];
        
        //设置body,需要转换为NSData
        NSDictionary *addRequestBody = @{@"title":@"How to Use RESTful API"};
        NSData *addRequestBodyData = [NSJSONSerialization dataWithJSONObject:addRequestBody options:NSJSONWritingPrettyPrinted error:nil];
        [addRequest setHTTPBody:addRequestBodyData];
        
        //发送请求
        NSHTTPURLResponse *response;
        NSData *data = [NSURLConnection sendSynchronousRequest:addRequest returningResponse:&response error:nil];
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        //打印请求结果
        NSLog(@"statusCode:%ld",(long)response.statusCode);
        NSLog(@"result:%@",result);
    

    得到的结果如下,从文档中的描述可以判断该请求是否成功响应:

    statusCode:201
    result:{
        createdAt = "2016-01-06 10:44:58";
        objectId = 8a3b9ffd9e;
    }
    

    另外,需要注意的是,如果发送的是GET请求,一般需要对参数先进行JSON编码再进行URL编码。例如使用条件查询,通过where参数进行约束,要查找出帖子的标题为“How to Use RESTful API”时,需要像下面代码那样构造请求。

        //构造URL字符串
        NSString *urlString = @"https://api.bmob.cn/1/classes/Post?where=";
        //查询条件JSON编码
        NSDictionary *queryDic = @{@"title":@"How to Use RESTful API"};
        NSData *queryData = [NSJSONSerialization dataWithJSONObject:queryDic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *queryString = [[NSString alloc] initWithData:queryData encoding:NSUTF8StringEncoding];
        urlString = [NSString stringWithFormat:@"%@%@",urlString,queryString];
        
        //URL编码
        NSString *urlEncode = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
        //设置URL
        NSURL *url = [NSURL URLWithString:urlEncode];
        NSMutableURLRequest *queryRequest = [[NSMutableURLRequest alloc] initWithURL:url];
        
        //设置请求方法
        [queryRequest setHTTPMethod:@"GET"];
        
        //设置请求头
        [queryRequest setAllHTTPHeaderFields:@{@"X-Bmob-Application-Id":APPKEY,@"X-Bmob-REST-API-Key":RESTFULKEY,@"Content-Type":@"application/json"}];
        
        //发送请求
        NSHTTPURLResponse *response;
        NSData *data = [NSURLConnection sendSynchronousRequest:queryRequest returningResponse:&response error:nil];
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        //打印请求结果
        NSLog(@"statusCode:%ld",(long)response.statusCode);
        NSLog(@"result:%@",result);
    

    总结

    本质上,使用RESTful API,就是通过构造不同的HTTP请求来达到与服务器交互的效果。上面使用的都是最基本的网络请求API,并且都采用了同步调用,我们也可以根据自己的需求使用异步调用的API或者是别人封装好的HTTP请求框架来使用RESTful API。

  • 相关阅读:
    操作系统01_进程和线程管理
    数据库02_字段类型
    鲁滨逊漂流记游戏
    查找数N二进制中1的个数(JS版 和 Java版)
    js中的call、apply
    jQuery对象与Dom对象的相互转换
    jndi配置数据源
    关于JS中变量的作用域-实例
    重写equals()方法时,需要同时重写hashCode()方法
    String与StringBuilder
  • 原文地址:https://www.cnblogs.com/limaofuyuanzhang/p/BmobRestfulAPIUse.html
Copyright © 2011-2022 走看看