【发布时间】:2015-01-19 02:28:45
【问题描述】:
我正在使用此函数向服务器发送请求并在 NSDictionary 中接收 JSON 响应。
- (void)performRequest:(NSString *)aRequest
{
NSString *string=[NSString stringWithFormat:@"%@%@",baseURL,aRequest];
NSURL *url = [NSURL URLWithString: string];
NSLog(@"string%@",string);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError)
{
if (data.length > 0 && connectionError == nil)
{
NSDictionary * greeting = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
NSLog(@"%@",greeting2);
}
}];
}
我想要修改它,使其保留一个发送服务 URL 的函数和一个单独的函数,其中 JSON 响应来自 NSDictionary,以便您可以在其他类中重用它们。
【问题讨论】:
标签: objective-c nsdictionary nsurlconnection nsurl