2014-09-25 99 views
5

每当我运行这个功能我得到断言错误,我应该怎么办断言失败 - [AFHTTPRequestSerializer requestWithMethod:URLString:参数:错误:]

[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' *第一次调用堆栈:

+0

什么该网址最终会成为? – borrrden 2014-09-25 05:50:54

回答

21

你需要添加这行代码,以避免无效参数的URL字符串

NSString *encoded = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
+0

working perfect – 2014-09-25 06:00:50

+0

像火箭一样工作! – Abo3atef 2014-11-09 11:11:18

+1

像火箭 – 2014-12-06 15:04:44

3
  • (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc NS_DEPRECATED(10_0, 10_11, 2_0, 9_0, "Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.");

@NullData在这一刻完美的回答工作,但方法stringByAddingPercentEscapesUsingEncoding已过时的话,你们应该考虑到改为使用stringByAddingPercentEncodingWithAllowedCharacters方法。

像这样迅速

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

或本作objc

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

月的URL地址不正确,有空间,错误的字符等。例如: enter image description here