gpt4 book ai didi

iphone - 向 NSMutableURLRequest 添加参数时出现异常

转载 作者:行者123 更新时间:2023-12-03 18:31:13 25 4
gpt4 key购买 nike

将 xml 正文添加到 POST 请求时出现以下异常。当我用 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'

最佳答案

NSMutableURLRequest *request = [NSURLRequest requestWithURL:...

应该是

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:...

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

关于iphone - 向 NSMutableURLRequest 添加参数时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com