【问题标题】:NSMutableURLRequest setHTTPBody: crashNSMutableURLRequest setHTTPBody:崩溃
【发布时间】:2011-04-07 09:37:03
【问题描述】:

我正在尝试将一些数据在线发送到 PHP 脚本,以便为我的 iOS 应用程序提交在线分数。

该脚本将要求帐户凭据格式为 HTTP 请求的正确转义字符。它是用于兑现应用程序的 setHTTPBody 方法:

        [text_field removeFromSuperview];
        text_field.text = @""; //Remove text so it doesn't show because the text field is still on top of the open gl view
        text_field.enabled = NO;
        NSMutableURLRequest * request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"secret"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
        [request setHTTPMethod: @"POST"];
        [request setHTTPBody: [[NSString stringWithFormat:@"username=%@&password=%@&score=%i",[ogl_view->username stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding],[text_field.text stringByAddingPercentEscapesUsingEncoding:NSUnicodeStringEncoding],ogl_view->score[0]] dataUsingEncoding:NSUnicodeStringEncoding]];  
        NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate:self];
        if (connection){
            received_data = [[NSMutableData data] retain];
            ogl_view->section = CURLING_PROCESS_CREDENTIALS;
        }else{
            ogl_view->section = CURLING_LOGIN_OR_REGISTER;
            UIAlertView *connection_alert = [[UIAlertView alloc] initWithTitle:@"Error" message: @"Can't connect to server" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil];
            [connection_alert show];
            [connection_alert release];
        }

我发现它在调用 setHTTPBody 方法时崩溃。有谁知道为什么?

感谢您的任何回答。

编辑:这是崩溃日志:

    2010-09-02 19:02:30.291 iPhone Monkey Curling[431:207] -[DOMText stringByAddingPercentEscapesUsingEncoding:]: unrecognized selector sent to instance 0x5c97ce0
    2010-09-02 19:02:30.293 iPhone Monkey Curling[431:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DOMText stringByAddingPercentEscapesUsingEncoding:]: unrecognized selector sent to instance 0x5c97ce0'
    *** Call stack at first throw:
    (
        0   CoreFoundation                      0x02531919 __exceptionPreprocess + 185
        1   libobjc.A.dylib                     0x0267f5de objc_exception_throw + 47
        2   CoreFoundation                      0x0253342b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
        3   CoreFoundation                      0x024a3116 ___forwarding___ + 966
        4   CoreFoundation                      0x024a2cd2 _CF_forwarding_prep_0 + 50
        5   iPhone Monkey Curling               0x00004338 -[PasswordFieldDelegate textFieldShouldReturn:] + 450
        6   UIKit                               0x00699358 -[UIKeyboardImpl callShouldInsertText:] + 148
        7   UIKit                               0x05f29c86 -[UIKeyboardImplAccessibility(SafeCategory) callShouldInsertText:] + 70
        8   UIKit                               0x006a0693 -[UIKeyboardImpl addInputString:fromVariantKey:] + 107
        9   UIKit                               0x006a2957 -[UIKeyboardImpl handleKeyEvent:] + 1723
        10  UIKit                               0x007c31c0 -[UIKeyboardLayoutStar sendStringAction:forKey:] + 684
        11  UIKit                               0x007c79ba -[UIKeyboardLayoutStar touchUp:] + 2556
        12  UIKit                               0x006b99a3 -[UIKeyboardLayout touchesEnded:withEvent:] + 550
        13  UIKit                               0x0058a2ff -[UIWindow _sendTouchesForEvent:] + 567
        14  UIKit                               0x0056c1ec -[UIApplication sendEvent:] + 447
        15  UIKit                               0x00570ac4 _UIApplicationHandleEvent + 7495
        16  GraphicsServices                    0x02d97afa PurpleEventCallback + 1578
        17  CoreFoundation                      0x02512dc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
        18  CoreFoundation                      0x02473737 __CFRunLoopDoSource1 + 215
        19  CoreFoundation                      0x024709c3 __CFRunLoopRun + 979
        20  CoreFoundation                      0x02470280 CFRunLoopRunSpecific + 208
        21  CoreFoundation                      0x024701a1 CFRunLoopRunInMode + 97
        22  GraphicsServices                    0x02d962c8 GSEventRunModal + 217
        23  GraphicsServices                    0x02d9638d GSEventRun + 115
        24  UIKit                               0x00574b58 UIApplicationMain + 1160
        25  iPhone Monkey Curling               0x00002388 main + 102
        26  iPhone Monkey Curling               0x00002319 start + 53
    )

【问题讨论】:

  • 如果它正在崩溃,那么有一个崩溃日志或来自调试器的回溯。请张贴出来。
  • 以前没有,但现在有了。奇怪的。我会发布日志。我不知道为什么它说 stringByAddingPercentEscapesUsingEncoding 方法无效。比萨雷。

标签: iphone objective-c ios


【解决方案1】:

-setHTTPBody 接受 NSData,而不是 NSString。在设置之前,您需要将 NSString 转换为 NSData

幸运的是,NSString 有一个 dataUsingEncoding: 方法让它变得超级简单。

【讨论】:

  • 谢谢。我已经实现了这一点。不幸的是,它仍然在崩溃。
  • 您的崩溃日志表明您正在尝试将 stringByAdding... 发送到不是 NSString 的东西。我会调查的。
  • ogl_view->username 绝对是一个 NSString ,所以如果文本字段上的文本属性......我将不得不深入研究它。既然你给我带来了另一个问题,我会接受你的回答作为解决方案,谢谢。这里的问题必须从其他地方开始......
  • 另一种方法中的一行导致问题:ogl_view->username = text_field.text;我需要弄清楚那条线现在在做什么。它位于文本视图的委托方法中。这是 textFieldShouldReturn 方法。
  • 我找到了解决方法,这个:ogl_view->username = text_field.text;应该是这样的:[ogl_view->username release]; //释放已经存在的字符串 ogl_view->username = [text_field.text retain];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多