0

每当我试图通过 xcode 7 中的 JSon 获取数据时,它都会给我错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

在 xcode 6 中它工作正常。是否在 xcode 7 中实现了从 json 获取数据的新方法。

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"]];


id response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
4

2 回答 2

0
NSError *error;
NSURL *url = [NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error:&error];
if (!data)
{
    NSLog(@"Download Error: %@", error.localizedDescription);

}

// Parsing the JSON data received from web service into an NSDictionary object
NSDictionary *jSON =
[NSJSONSerialization JSONObjectWithData: data
                                options: NSJSONReadingMutableContainers
                                  error: &error];

试试这个它的工作

于 2015-06-15T10:31:45.247 回答
-1

在 iOS 9.0 中它将不再支持 http:// 它需要 https://

将此添加到 info.plist 可以解决问题,但不知道苹果是否允许此类应用在应用商店中发布。

< key>NSAppTransportSecurity< / key>
     < dict>
        < key>NSAllowsArbitraryLoads< / key>< true/>
     < / dict>

通过在文本编辑器中编辑 info.plist 添加它是行不通的,必须从 xcode 本身做。(我是编程新手)

于 2015-06-15T11:37:13.323 回答