ios - POST请求响应?

标签 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

最佳答案

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

关于ios - POST请求响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560975/

相关文章:

ios - 像 Lyft 应用程序一样集中放置 ImageView

ios - NSMutableArray 与 UITableViewCell

ios - 在 Objective-C 中从 UnsafeBufferPointer 创建一个数组

ios - 哪个最好 :calling a property by @synthesize or by using self keyword

android - 如何使用 SyncAdapter 处理远程服务器的 RESTful 更新

ios - 使用自定义 UITableViewCell 时,删除确认按钮不会消失

ios - objective-c :没有@interface声明选择器错误

objective-c - 在 XCode 6 中对 Google Analytics 跟踪器进行单元测试时出现错误

api - 验证客户端和保护 RESTful api 的通用方法

到 "pay for product"的 RESTful 方式