繁体   English   中英

iOS-使用REST API(POST)在Parse.com中存储对象

[英]IOS - Store an object in Parse.com with REST API (POST)

我正在尝试学习如何使用Parse REST API,并且在使用POST请求存储新数据时遇到一些问题。 我没有使用AFNetworking,而是通常使用的一类。 我以为这堂课可能有错误,但是我没有造成任何麻烦地执行了GET请求。

我收到此错误:

输出数据:{代码= 107; 错误=“无效的JSON”;

ViewController.m

    NSMutableDictionary * dictionary = [[NSMutableDictionary alloc]init];
[dictionary setObject:@"Adriel" forKey:@"name"];
[dictionary setObject:@"tralala" forKey:@"icon"];
[dictionary setObject:@"He is bla bla bla bla bla " forKey:@"bio"];


[KPWebServiceController connectWithPOSTtoAPIURL:@"classes/User" withParams:dictionary andResponse:^(NSURLResponse *serviceResponse, id receivedData, NSError *error) {
    NSLog(@"Response %@", receivedData);

}];

方法:

+ (void)connectWithPOSTtoAPIURL:(NSString *)APIURLString
                     withParams:(NSMutableDictionary *)params
                    andResponse:(void (^)(NSURLResponse *serviceResponse, id receivedData, NSError *error))responseHandler{

        NSError *error;


        NSString *venuesQuery = [[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&error]
                                                   encoding:NSUTF8StringEncoding] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"Request %@", venuesQuery);

        NSMutableURLRequest *parseRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@/?%@%@", ParseRestAPIURL, APIURLString, @"where=", venuesQuery]]];
        [parseRequest setHTTPMethod:@"POST"];
        [parseRequest setValue:ParseApplicationID forHTTPHeaderField:@"X-Parse-Application-Id"];
        [parseRequest setValue:ParseRestAPIID forHTTPHeaderField:@"X-Parse-REST-API-Key"];

        NSLog(@"Request %@", parseRequest);

        [NSURLConnection sendAsynchronousRequest:parseRequest
                                           queue:[NSOperationQueue mainQueue]
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {


                                   id receivedData;
                                   if (connectionError) {
                                       NSLog(@"CONNECTION ERROR: %@", connectionError.localizedDescription);
                                   }

                                   else {
                                       NSError *jsonError;
                                       receivedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
                                       if (jsonError) {
                                           NSLog(@"JSON ERROR INPUT: %@",jsonError);
                                       }else {
                                           NSLog(@"OUTPUT DATA: %@", receivedData);
                                       }
                                   }

                                   responseHandler (response,receivedData,connectionError);
                               }];

}

我用谷歌搜索了问题,并在SO中进行了检查,但这是我解决(不是)解决问题的程度,

我正在尝试进行一些更正,但问题不在于JSON字符串本身。

我修改了我的方法,删除了此编码行,并且效果很好。 (是的,我不知道我做了XD)

+ (void)connectWithPOSTtoAPIURL:(NSString *)APIURLString
                     withParams:(NSDictionary *)params
                  withKeyObject:(NSString *)keyObject
                    andResponse:(void (^)(NSURLResponse *serviceResponse, id receivedData, NSError *error))responseHandler{


    NSError *jsonError;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&jsonError];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", ParseRestAPIURL, APIURLString]];
    NSMutableURLRequest *dataRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    [dataRequest setTimeoutInterval:30.0];
    [dataRequest setHTTPMethod:@"POST"];
    [dataRequest setValue:ParseApplicationID forHTTPHeaderField:@"X-Parse-Application-Id"];
    [dataRequest setValue:ParseRestAPIID forHTTPHeaderField:@"X-Parse-REST-API-Key"];
    [dataRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
    [dataRequest setHTTPBody:jsonData];


    [NSURLConnection sendAsynchronousRequest:dataRequest
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

                               id receivedData;
                               if (connectionError) {
                               }

                               else {
                                   NSError *jsonError;
                                   receivedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
                                   if (jsonError) {
                                   }else {
                                       // NSLog(@"OUTPUT DATA: %@", receivedData);
                                   }
                               }

                               responseHandler (response,receivedData,connectionError);
                           }];

}

我得到了这个(很棒的)回复:

响应{createdAt =“ 2015-08-03T04:01:18.024Z”; objectId = sNqhMkX5yv; }

好极了! :)

我希望这会对其他XD有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM