zoukankan      html  css  js  c++  java
  • IOS 'NSInternalInconsistencyException'

    今天想写一个请求的天气。好的。废话不多说。先贴代码:

    使用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 中,不然链接里的那些&符号。好像不识别把!



  • 相关阅读:
    Java虚拟机基础
    排序系列之插入排序
    排序系列之冒泡排序
    成为优秀程序员的10个要点
    23个适合Java开发者的大数据工具和框架
    成为一个更优秀的开发者的10种方式
    Mybatis自动生成实体类
    Maven-SSM项目pom.xml配置以及springmvc配置以及mybatis配置及web.xml配置
    SSM项目layui分页实例
    图书管理系统(毕业论文)
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7041391.html
Copyright © 2011-2022 走看看