转载自:http://i.cnblogs.com/EditPosts.aspx?postid=4012011
今天想写一个请求的天气,好的,废话不多说,先贴代码:
使用AFNetWorking 发送get请求,但是一直报错 IOS 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: URLString'
翻译出来就是 不能满足urlstring, 可能时请求地址错了,但是请求地址没错,返回是一串json数据,然后我就迷糊了,后来 我发现这个url中参数是直接写上去的
,然后parameters 放参数的地方 没放,后来我把参数单独写了进来,就搞定了啊!
[appDelegate.manager GET:@"http://api.map.baidu.com/telematics/v3/weather?location=南京&output=json&ak=4zG5R7SqnQa" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *rootDict=responseObject; NSLog(@"%@",rootDict); NSArray *resultArray = [rootDict objectForKey:@"results"]; NSDictionary *cityDict=[resultArray objectAtIndex:0]; //获取城市 NSString *currentCity= [cityDict objectForKey:@"currentCity"]; //准备获取天气 NSArray *weatherArray= [cityDict objectForKey:@"weather_data"]; //获取第一天天气的字典 NSDictionary *firstDict=[weatherArray objectAtIndex:0]; //获取第一天日期 NSString *firstDate=[firstDict objectForKey:@"date"]; //获取第一天天气 NSString *weather=[firstDict objectForKey:@"weather"]; //获取第一天风向 NSString *wind=[firstDict objectForKey:@"wind"]; //获取第一天气温 NSString *temper=[firstDict objectForKey:@"temperature"]; [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"当前城市%@ 日期:%@ 天气%@ 风向%@ 气温%@ ",currentCity,firstDate,weather,wind,temper] message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"链接失败"); }]; });
正确代码:
NSDictionary *parameter=@{@"location": @"南京",@"output": @"json",@"ak": @"4zG5R7Lw8Fd3SqnQa"}; [appDelegate.manager GET:@"http://api.map.baidu.com/telematics/v3/weather" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
这里的参数一定要写再 parameters 中,不然链接里的那些&符号,好像不识别把!