我試圖獲取JSON數據並使用NSURL會分析它,但得到空每一次 - 而且除了我想o顯示在表視圖中的所有數據如何在Objective-C中使用NSUrl會話獲取數據?
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/2iodh4vg0eortkl/facts.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"text/plain" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"GET"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"Value", @"Key", nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Resopnse == %@",response);
if (response != nil) {
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"JSon dict === %@",jsonDict);
}
}];
[postDataTask resume];
我可以知道我做錯了有什麼其他的好方法可以做同樣的事嗎?
什麼是空? '迴應'? '數據'? 'jsonDict'?通常,您不要將GET的參數放入HTTPBody中。 – Larme