向NSMutableURLRequest添加参数时出现异常

时间:2011-03-02 14:25:55

标签: iphone ipad post get

在向POST请求添加xml正文时,我收到以下异常。在使用GET执行相同操作时,我得到了相同的异常。任何帮助将不胜感激。

代码

- (IBAction)buttonTapped:(id)sender {
NSLog(@"buttonTapped");

//Creating the request
NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.1:9081/Transport/services/DriverService"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 30.0];
[ request setHTTPMethod: @"POST"];
//creating connection and sending it
NSURLConnection *connection = [ [NSURLConnection alloc] initWithRequest:request delegate:self];

NSString *xmlString = @"<Driver type=\"GetName\"><DriverID>1</DriverID></Driver>";

[request setHTTPBody:[xmlString 
                      dataUsingEncoding:NSUTF8StringEncoding]];

//checking if connection is good
if (connection){
    NSLog (@"Connection successfull!");
    self.responseData = [NSMutableData data];
}
else
{
    NSLog (@"Connection failed");
}
}

异常

Exception:
2011-03-02 09:22:12.055 XML[1125:207] buttonTapped

2011-03-02 09:22:12.056 XML[1125:207] -[NSURLRequest setHTTPMethod:]: unrecognized selector sent to instance 0x4b1a970

2011-03-02 09:22:12.057 XML[1125:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLRequest setHTTPMethod:]: unrecognized selector sent to instance 0x4b1a970'

*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dafbe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f045c2 objc_exception_throw + 47
    2   CoreFoundation                      0x00db16fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d21366 ___forwarding___ + 966
    4   CoreFoundation                      0x00d20f22 _CF_forwarding_prep_0 + 50
    5   XML                                 0x00002bc4 -[XMLViewController buttonTapped:] + 164
    6   UIKit                               0x002b8a6e -[UIApplication sendAction:to:from:forEvent:] + 119
    7   UIKit                               0x003471b5 -[UIControl sendAction:to:forEvent:] + 67
    8   UIKit                               0x00349647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    9   UIKit                               0x003481f4 -[UIControl touchesEnded:withEvent:] + 458
    10  UIKit                               0x002dd0d1 -[UIWindow _sendTouchesForEvent:] + 567
    11  UIKit                               0x002be37a -[UIApplication sendEvent:] + 447
    12  UIKit                               0x002c3732 _UIApplicationHandleEvent + 7576
    13  GraphicsServices                    0x016e5a36 PurpleEventCallback + 1550
    14  CoreFoundation                      0x00d91064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    15  CoreFoundation                      0x00cf16f7 __CFRunLoopDoSource1 + 215
    16  CoreFoundation                      0x00cee983 __CFRunLoopRun + 979
    17  CoreFoundation                      0x00cee240 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x00cee161 CFRunLoopRunInMode + 97
    19  GraphicsServices                    0x016e4268 GSEventRunModal + 217
    20  GraphicsServices                    0x016e432d GSEventRun + 115
    21  UIKit                               0x002c742e UIApplicationMain + 1160
    22  XML                                 0x000028dc main + 102
    23  XML                                 0x0000286d start + 53
)
terminate called after throwing an instance of 'NSException'

1 个答案:

答案 0 :(得分:37)

NSMutableURLRequest *request = [NSURLRequest requestWithURL:...

应该是

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:...

虽然您声明请求具有NSMutableURLRequest类型,但您创建了NSURLRequest类的实例,该实例不响应-setHTTPMethod:选择器。