我在Json上使用GET方法。 GET方法在for循环中,问题是它没有完成任务或没有得到结果。相反,循环递增。我在块中放置了一个断点,我将结果数据设置为NSDictionary,但它永远不会去那里。
是否可以直接调用GET方法。我的意思是代码将逐行读取。并且它不会跳过或等待json完成处理?
这就是我所做的:
- (void)downloadJsonFeed
{
for(int i = 1;i < self.numberOfEpisodes;i++)
{
NSString *endPoint = [[[[baseJsonUrl stringByAppendingString:getEpisodes]stringByAppendingString:self.title]stringByAppendingString:@"&episodeNumber="]stringByAppendingString:[NSString stringWithFormat:@"%i",i]];
NSLog(@"End point %@",endPoint);
[JsonDownload getJson:token andEndpointString:endPoint WithHandler:^(__weak id result)
{
NSArray *episodeArray =result;
//will do some task here
}];
}
}
- (void)getJson:(NSString *)authData andEndpointString:(NSString *)urlString WithHandler:(void(^)(__weak id result))handler
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
//NSString *auth = [NSString stringWithFormat:@"Bearer {%@}", authData];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:authData forHTTPHeaderField:@"Cookie"];
[urlRequest setHTTPMethod:@"GET"];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
id returnedObject = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error:nil];
handler(returnedObject);
}
else{
NSLog(@"error %@",error);
}
}];
[dataTask resume];
}
我在这一行NSArray *episodeArray =result;
中设置了一个断点,它永远不会去那里。但是,当我将断点放在[JsonDownload getJson:token andEndpointString:endPoint WithHandler:^(__weak id result)
行时,它正在响应
在注释行//will do some task here
上,我需要在那里完成另一个json之前的任务。但我不能让它永远不会进入代码块
答案 0 :(得分:0)
通过用%20
替换endPoint中的空格字符来解决此问题