轉載自: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:@"當前城市%@\n日期:%@\n天氣%@\n風向%@\n氣溫%@\n",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 中,不然鏈接里的那些&符號,好像不識別把!