我看过很多关于 nsurl 异步的教程。我遵循了这些教程并实施了以下操作。
-(id) connect_asych:(NSDictionary *)input page:(NSString *)page{
NSString* urlString= [@"*s.com/music/" stringByAppendingString:page];
NSURL *url = [NSURL URLWithString:urlString];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:@"POST"];
//initialize a post data
NSString *post = [self convert:input];
//set request content type we MUST set this value.
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//set post data of request
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error1) {
if(error !=nil){
_responseData=nil;
}
[_responseData appendData: data];
NSLog(@"%@",_responseData);
}];
id object = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingAllowFragments error:&error];
if(error !=nil){
_error=[[NSString alloc] initWithFormat:@"error"];
return error;
}
return object;
}
如果我的viewdidload,我调用了上面的方法。
我通过使用同步方法成功地从数据库中获取了数据。问题是当我使用异步方法时,我无法获取数据。我应该在 viewdidload 中调用异步方法吗?