我目前正在尝试从网址获取天气信息,并将其用于字符串或数据文件,我可以使用该文件在屏幕上使用标签显示信息。
我根据迄今为止的回答编辑了以下内容;我到目前为止的代码如下:
// Method 1
NSString *path = @"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *s = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // This line triggers the exception error
// method 2
NSString *path = @"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSDictionary *s = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; // this line triggers the exception error
// Method 3
NSString *path = @"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b";
NSURL *url = [NSURL URLWithString:path];
NSError *error = nil;
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; // this line triggers exception error
/*
if (error)
NSLog(@"JSONObjectWithData error: %@", error);
for (NSMutableDictionary *dictionary in array)
{
NSString *arrayString = dictionary[@"array"];
if (arrayString)
{
NSData *data = [arrayString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
dictionary[@"array"] = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error)
NSLog(@"JSONObjectWithData for array error: %@", error);
}
}
*/
// Method 4
//-- Make URL request with server
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//-- Get request and response though URL
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//-- JSON Parsing
NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; // This line triggers the exception error
//NSLog(@"Result = %@",result);
我已经注释掉或分离了部分,因为我收到异常处理程序错误,并将其缩小到此行(或不同方法中的等效项):
NSMutableDictionary *s = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
它可能与JSON数据,格式,源或可能需要在访问之前更改设置或权限有关。我确实用方法4得到了这条消息: “pp Transport Security阻止了一个明文HTTP(http://)资源加载,因为它不安全。可以通过你的应用程序的Info.plist文件配置临时异常。” 但是我不知道我需要做什么编辑,它说是暂时的,所以我认为不是你无论如何都要修复它。我甚至不知道这是否与我的问题有关,或者我将不得不面对的其他问题。
有问题的JSON数据如下所示:
{
"coord":{"lon":-0.13,"lat":51.51},
"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],
"base":"stations",
"main":
{"temp":282.4,"pressure":1014,"humidity":76,"temp_min":281.15,"temp_max":284.15},
"visibility":10000,
"wind":{"speed":4.1,"deg":280},
"clouds":{"all":20},
"dt":1486471800,
"sys":
{"type":1,"id":5091,"message":0.004,"country":"GB","sunrise":1486452468,"sunset":1486486938},
"id":2643743,
"name":"London",
"cod":200
}
最终我希望能够通过以下方式访问它:
NSString *temperature =s[@"temperature"];
NSLog(@"%@", temperature);
NSString *humidity = [s objectForKey:@"humidity"];
非常感谢任何进一步的协助; 在此先感谢,并已经指导我了:)
答案 0 :(得分:3)
更改此代码:
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSMutableDictionary *s = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
进入这个:
NSData *data = [NSData dataWithContentsOfURL:path];
NSError *error = nil;
NSDictionary *s = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
<强>更新强>
由于这两行,您仍然会收到错误:
NSString *path = @"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b";
NSURL *url = [NSURL URLWithString:path];
您的网址为零,因为您的路径包含特殊字符。 将这两行改为:
NSString *path = [@"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:path];
答案 1 :(得分:2)
最有可能的数据为零。 JSONObjectWithData认为编程错误的一件事是传递nil;它抛出异常的时候。我不认为initWithContentsOfFile接受不是文件URL的URL。
顺便说一句。结果绝对不是NSMutableDictionary。 可能是一个NSDictionary,但你需要检查一下。而不是[s objectForKey:@&#34;温度&#34;]而不是写s [@&#34;温度&#34;]。
答案 2 :(得分:0)
你有三个问题:
以下是更正后的代码:
NSString *path = @"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSDictionary *s = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
答案 3 :(得分:0)
你设法禁用ATS吗?尝试将以下内容添加到Info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
我还会添加数据检查:
NSString *path = @"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSAssert(data, @"No data received!");
NSDictionary *s = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];