gpt4 book ai didi

iOS 应用程序开发- NSMutableURLRequest 为零

转载 作者:行者123 更新时间:2023-11-29 02:47:31 27 4
gpt4 key购买 nike

我是 iOS 开发的新手。现在我面临一个奇怪的问题。当我在 iPhone 5 模拟器上运行以下代码时,一切正常。然而,它在我真正的 iPhone 5 上不起作用。有人可以帮我帮我找出原因吗? objective-C : NSError *错误;

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@1 forKey:@"driverId"];
[dictionary setValue:@1 forKey:@"busId"];
[dictionary setValue:@1 forKey:@"lineId"];

NSData *postData = nil;
NSString *requestData = @"";
NSString *urlPath = @"myurl";
if([NSJSONSerialization isValidJSONObject:dictionary]){
postData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
NSString *str = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
requestData = [requestData stringByAppendingString:str];

postData = [NSData dataWithBytes:[requestData UTF8String] length:[requestData length]];
}

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:urlPath];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
NSString *status = [responseDic objectForKey:@"status"];

通过对比“request”和“nil”,我发现 request 是 nil。

错误是:

2014-07-23 19:13:54.698 ShuttleBusForDriver[2958:60b] 无请求2014-07-23 19:13:54.699 ShuttleBusForDriver[2958:60b] 响应日期:(空)2014-07-23 19:13:54.701 ShuttleBusForDriver[2958:60b] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“数据参数为零”* 首先抛出调用堆栈:(0x2fc96ecb 0x3a431ce7 0x2fc96e0d 0x30624967 0xdfa55 0xde785 0x324cca33 0x324cc7f1 0x32658bf3 0x3257646f 0x32576279 0x32576211 0 x324c82e5 0x3214431b 0x3213fb3f 0x3213f9d1 0x3213f3e5 0x3213f1f7 0x324cb99f 0x2fc61faf 0x2fc61477 0x2fc5fc67 0x2fbca729 0x2fb ca50b 0x34b396d3 0x3252b871 0xdc69c 0xdc6d8 0x3a92fab7)libc++abi.dylib:以 NSException 类型的未捕获异常终止

最佳答案

@JingDu 首先抱歉,由于我的 stackoverflow 网站问题,我无法添加评论。符号化的崩溃报告是符号化的报告,它看起来像这样:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(
0 CoreFoundation 0x0284d1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01c7e8e5 objc_exception_throw + 44
2 CoreFoundation 0x0284cfbb +[NSException raise:format:] + 139
3 Foundation 0x017b7bd2 +[NSJSONSerialization JSONObjectWithData:options:error:] + 67
4 Test 0x00003eb8 -[ViewController viewDidLoad] + 136
5 UIKit 0x0085033d -[UIViewController loadViewIfRequired] + 696
6 UIKit 0x008505d9 -[UIViewController view] + 35
7 UIKit 0x00770267 -[UIWindow addRootViewControllerViewIfPossible] + 66
8 UIKit 0x007705ef -[UIWindow _setHidden:forced:] + 312
9 UIKit 0x0077086b -[UIWindow _orderFrontWithoutMakingKey] + 49
10 UIKit 0x0077b3c8 -[UIWindow makeKeyAndVisible] + 65
11 UIKit 0x0072bbc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
12 UIKit 0x00730667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
13 UIKit 0x00744f92 -[UIApplication handleEvent:withNewEvent:] + 3517
14 UIKit 0x00745555 -[UIApplication sendEvent:] + 85
15 UIKit 0x00732250 _UIApplicationHandleEvent + 683
16 GraphicsServices 0x0255cf02 _PurpleEventCallback + 776
17 GraphicsServices 0x0255ca0d PurpleEventCallback + 46
18 CoreFoundation 0x027c8ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
19 CoreFoundation 0x027c89db __CFRunLoopDoSource1 + 523
20 CoreFoundation 0x027f368c __CFRunLoopRun + 2156
21 CoreFoundation 0x027f29d3 CFRunLoopRunSpecific + 467
22 CoreFoundation 0x027f27eb CFRunLoopRunInMode + 123
23 UIKit 0x0072fd9c -[UIApplication _run] + 840
24 UIKit 0x00731f9b UIApplicationMain + 1225
25 Test 0x000026bd main + 141
26 libdyld.dylib 0x02d30725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException

它来自与您类似的代码:

 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' * First throw call stack: (0x2fc96ecb 0x3a431ce7 0x2fc96e0d 0x30624967 0xdfa55 0xde785 0x324cca33 0x324cc7f1 0x32658bf3 0x3257646f 0x32576279 0x32576211 0x324c82e5 0x3214431b 0x3213fb3f 0x3213f9d1 0x3213f3e5 0x3213f1f7 0x324cb99f 0x2fc61faf 0x2fc61477 0x2fc5fc67 0x2fbca729 0x2fbca50b 0x34b396d3 0x3252b871 0xdc69c 0xdc6d8 0x3a92fab7) libc++abi.dylib: terminating with uncaught exception of type NSException

关于iOS 应用程序开发- NSMutableURLRequest 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24923975/

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