POST请求的响应?

时间:2016-02-22 18:20:19

标签: ios objective-c rest post

我正在尝试使用消息正文中的某些参数构造一个POST请求,但我正在努力处理/查看响应。

这是我试图在Obj C中重新创建的请求:

enter image description here

我对创建此请求的代码有些熟悉,但我正在努力将响应解析为有意义的数据。这就是我的方法目前的样子(看似成功):

  NSLog(@"WEB SERVICE CALLED!");

    NSURL *aUrl = [NSURL URLWithString:@"xxx"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request setHTTPMethod:@"POST"];
    NSString *postString = @"grant_type=password&username=xxx&password=xxx";
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
                                                                 delegate:self];

    if(connection) {
        NSLog(@"Connection Successful");
    } else {
        NSLog(@"Connection could not be made");
    }
}

如何查看我的回复?在此先感谢您的帮助!

更新

我添加了这个位,可以看到我的响应给了我一个代码200(成功),但数据对象看起来需要序列化,因为它看起来像一个十六进制字符串。这就是我正在处理的响应/数据/错误:

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

                           // your data or an error will be ready here
                           NSLog(@"RESPONSE: %@",response);

                           NSLog(@"DATA: %@",data);

                           NSLog(@"ERROR: %@",error);
                       }];

这是数据对象的日志: enter image description here

1 个答案:

答案 0 :(得分:5)

NSError *error;
     id obj= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
     if (!obj) {
         NSLog(@"JSON parse error: %@", error);
     } else {
         NSLog(@"obj= %@", obj);
     }
相关问题