我目前正在尝试从URL中获取天气信息,并获取一个字符串或数据文件,以便使用标签在屏幕上显示该信息。
根据到目前为止的回答,我编辑了以下内容:到目前为止,我的代码如下:
// 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传输安全性由于不安全而阻止了明文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"];
任何进一步的帮助将不胜感激;
在此先感谢和那些已经指导了我的人:)
最佳答案
更改此代码:
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];
您的网址为nil,因为您的路径包含特殊字符。
将这两行更改为:
NSString *path = [@"http://api.openweathermap.org/data/2.5/weather?q={London}&appid=4737e39e801a13bd10da52d8837e470b" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:path];