断言失败 - [AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:]

时间:2014-09-25 05:42:03

标签: ios afnetworking-2

每当我运行此函数时,我都会收到断言错误,我应该怎么做

   [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *link = [NSString stringWithFormat:@"%@device_model_no=%@&device_serial_no=%@&device_imei=%@&note=%@&disclaimer=%@&extra_note=%@&package_plan=%@&price=%@&duration=%@&device=%@&user_reg_id=%@&device_category=%@",KURL,txtModelNumber.text,txtSerialNo.text,txtIMEI.text,txtViewExtra.text,deviceDisclamer,additonalDetail.text,plan,price,duration,deviceName,userID,nil];
    NSLog(@"%@",link);

    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    [manager GET:link parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);


        NSLog(@"%@", responseObject);

        NSDictionary *returnedDealDict = responseObject ;
        NSArray *returnArray = [returnedDealDict objectForKey:@"result"];

        for(NSDictionary *dealDict in returnArray)
        {
            if ([dealDict objectForKey:@"success"])
            {
                ALERT_VIEW(@"Thanks1! Your information has been saved Successfully! We will contact you soon.")
            }
            else
            {
                ALERT_VIEW(@"Your information doesn t exist in our database, Please go back and enter your contact info again!")
            }
        }
        success();

        [MBProgressHUD hideHUDForView:self.view animated:YES];




    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             ALERT_VIEW(@"Error occured while loading data.");
             NSLog(@"getBrandDevice Error");
             [MBPro

gressHUD hideHUDForView:self.view animated:YES];
             }];

我收到此错误

** Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:], /Users/ios2/Desktop/untitled folder/PhablatFIx/PhablatFIx/MVC/Model/AFNetworking/AFURLRequestSerialization.m:277
    2014-09-25 10:33:26.733 PhablatFIx[721:90b]

*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效的参数不满足:URLString'     * 首先抛出调用堆栈:

3 个答案:

答案 0 :(得分:21)

你需要添加这行代码以避免无效的参数url string

 NSString *encoded = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:3)

  
      
  • (可为空的NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc NS_DEPRECATED(10_0,10_11,2_0,9_0,"使用   -stringByAddingPercentEncodingWithAllowedCharacters:相反,它始终使用推荐的UTF-8编码,并编码为   特定的URL组件或子组件,因为每个URL组件或   子组件对于哪些字符有效具有不同的规则。");
  •   

@NullData回答此时工作完美,但方法stringByAddingPercentEscapesUsingEncoding已弃用,所以你们应该考虑使用stringByAddingPercentEncodingWithAllowedCharacters方法。

对于swift来说,这是一样的

let path = myurlstring.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())

或对于objc

path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

答案 2 :(得分:2)

可能是url地址不正确,有空格,错误字符等。例如: enter image description here