我试图构建一个带有消息正文中某些参数的POST请求,但在处理/查看响应时却很费劲。
这是我试图在Obj中重新创建的请求:

我对创建此请求的代码有些熟悉,但我很难将响应解析为有意义的数据。这就是我目前的方法(它看起来很成功):
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);
}];数据对象的日志如下所示:

发布于 2016-02-22 18:43:21
NSError *error;
id obj= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!obj) {
NSLog(@"JSON parse error: %@", error);
} else {
NSLog(@"obj= %@", obj);
}https://stackoverflow.com/questions/35560975
复制相似问题