NSMutableURLRequest setHTTPBody方法使用SIGABRT警告崩溃应用程序

时间:2012-09-10 01:25:24

标签: objective-c ios ios5 nsmutableurlrequest

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"riderfinder.appspot.com/login"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/plain" 
   forHTTPHeaderField:@"Content-type"];

    NSString *body = @"username=";
    [body stringByAppendingString:accountEntered];
    [body stringByAppendingString:@"&"];
    [body stringByAppendingString:@"password="];
    [body stringByAppendingString:passwordEntered];

    NSMutableData *data = [[NSMutableData data] initWithString:body];

    //Crashes everything with "SIGABRT" warning/error. Nothing else is said.
    [request setHTTPBody:data];

如果有人知道出了什么问题,我将不胜感激。我把它缩小到最后一行,导致Apple的调试器崩溃。非常感谢你!

2 个答案:

答案 0 :(得分:1)

有2个错误:

    未附加
  1. 正文[body stringByAppendingString:accountEntered]应为body=[body stringByAppendingString:accountEntered]

  2. NSMutableData *data = [[NSMutableData data] initWithString:body];未正确使用,您可以使用NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding];

  3. 所以我修改了代码:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"riderfinder.appspot.com/login"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"text/plain" forHTTPHeaderField:@"Content-type"];
    
    NSString *body = @"username=";
    body=[body stringByAppendingString:accountEntered];
    body=[body stringByAppendingString:@"&"];
    body=[body stringByAppendingString:@"password="];
    body=[body stringByAppendingString: passwordEntered];
    
    //NSMutableData *data = [[NSMutableData data] initWithString:body];
    NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding];
    
    [request setHTTPBody:data];
    

答案 1 :(得分:0)

首先,你应该让'accountEntered'并且' passwordEntered'不是。 如果你已经这样做了,你应该设置断点以确切地知道哪条线路崩溃。通常," SIGABRT"错误意味着您释放更多或未识别的选择器。 答案1的代码是对的。