標籤:ios json nsstring
今天想寫一個請求的天氣,好的,廢話不多說,先貼代碼:
使用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 *[email protected]{@"location": @"南京",@"output": @"json",@"ak": @"4zG5R7Lw8Fd3SqnQa"}; [appDelegate.manager GET:@"http://api.map.baidu.com/telematics/v3/weather" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
這裡的參數一定要寫再 parameters 中,不然連結裡的那些&符號,好像不識別把!
IOS 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: URLString'