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



我正试图学习如何使用Parse REST API,我有一个POST请求,存储新数据的一些问题。我使用的不是AFNetworking,而是我通常使用的一个类。我想也许这个类有一个错误,但我做了一个GET请求没有不便。

我得到这个错误:

输出数据:{代码= 107;error = "invalid 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;}

耶!:)

希望对大家有所帮助。

最新更新