【发布时间】:2017-01-12 06:14:14
【问题描述】:
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSOperationQueue *downloadOperationQueue = [[NSOperationQueue alloc] init];
[downloadOperationQueue cancelAllOperations];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:Url]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"Respose %@",response.URL);
NSString *jsonInString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"json in string for backend: %@",jsonInString);
[self.delegate responseConnection:data withMethodName:methodName];
if (!error)
{
// did finish logic here, then tell the caller you are done with success
// completion(YES, nil);
}
else
{
// otherwise, you are done with an error
// completion(NO, error);
}
}];
【问题讨论】:
-
你为什么不用
NSURLSession? -
是的,我有那段代码,但你能告诉我用下面的代码得到它吗:
-
NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (error) { } else { } }];
标签: ios objective-c nsoperation nsoperationqueue nsjsonserialization