arc*_*e16 1 rest post objective-c ios
我正在尝试使用消息正文中的某些参数构造一个POST请求,但我正在努力处理/查看响应.
这是我试图在Obj C中重新创建的请求:
我对创建此请求的代码有些熟悉,但我正在努力将响应解析为有意义的数据.这是我的方法目前看起来像这样(它似乎是成功的):
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能看到我的回答是什么?在此先感谢您的帮助!
UPDATE
我添加了这个位,可以看到我的响应给了我一个代码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);
}];
Run Code Online (Sandbox Code Playgroud)
NSError *error;
id obj= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!obj) {
NSLog(@"JSON parse error: %@", error);
} else {
NSLog(@"obj= %@", obj);
}
Run Code Online (Sandbox Code Playgroud)