gpt4 book ai didi

ios - 来自 NSMutablearray 的 setheader 值

转载 作者:行者123 更新时间:2023-11-29 11:37:00 25 4
gpt4 key购买 nike

我在从 NSMutableArray 中获取值时遇到了问题。我想动态解析 requestHeader 的值。我在服务器上发布数据。

喜欢:

[theRequest setValue:@"application/json"  forHTTPHeaderField:@"Content-Type"];

如果我对每个部分单独使用,这将起作用。但是如果我想用 NSMutableArray 尝试相同的值,这给了我异常(exception)。

代码

for (int j = 0; j < arraySize; j=j+2)
{
NSLog (@"Element = %@", [randomSelection objectAtIndex: j]);
NSLog (@"Element = %@", [randomSelection objectAtIndex: j+1]);
NSString * setVal = [randomSelection objectAtIndex: j];
NSString * setHead = [randomSelection objectAtIndex: j+1];
[theRequest setValue:setVal forHTTPHeaderField:setHead];

}

下面是我的文件的完整代码。

//username & password  allow
NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
NSString *jsonStrq = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"jsonStrq: %@",jsonStrq);


NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:requestUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[theRequest setHTTPMethod:@"POST"];

//long count = [randomSelection count];
//here is the issue
for (int j = 0; j < arraySize; j=j+2)
{
NSLog (@"Element = %@", [randomSelection objectAtIndex: j]);
NSLog (@"Element = %@", [randomSelection objectAtIndex: j+1]);
NSString * setVal = [randomSelection objectAtIndex: j];
NSString * setHead = [randomSelection objectAtIndex: j+1];
[theRequest setValue:setVal forHTTPHeaderField:setHead];

}


//commented code [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[theRequest setHTTPBody:[jsonStrq dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
[NSURLConnection sendAsynchronousRequest:theRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){

if (error)
{
printf("ios Error send value %s", [error localizedDescription]);
}
else
{
sendHttpData(cHttpClient,[data length],[data bytes],200);
printf("data length %d",[data length]);
}

}];


NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];



NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError];
NSLog(@"url to send request= %@",theResponseData);
NSLog(@"%@",dataDictionaryResponse);

记录错误:

018-02-07 10:10:36.148563+0530 adwitiyaios[297:37666] Task <B384CA36-FFE9-43F5-851A-09926637E202>.<0> HTTP load failed (error code: -1005 [4:-4])
2018-02-07 10:10:36.152646+0530 adwitiyaios[297:37797] NSURLConnection finished with error - code -1005
ios Error send value hЪ\2622018-02-07 10:10:36.156287+0530 adwitiyaios[297:37730] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x1818f2364 0x180b38528 0x1818f22ac 0x182255f34 0x10005519c 0x100157074 0x100156be0 0x100153aa8 0x10018b258 0x1001ddc60 0x10018b3e8 0x10018bb2c 0x1001f7c44 0x100208ea0 0x1815182b4 0x181518180 0x181516b74)
libc++abi.dylib: terminating with uncaught exception of type NSException
2018-02-07 10:11:04.988149+0530 adwitiyaios[297:37990] XPC connection interrupted

最佳答案

NSURLRequest header 是一个 NSDictionary,因此您可以填写自定义 header 字典并使用方法 setAllHTTPHeaderFields 将自定义 header 设置为您的要求

尝试使用这个

NSMutableDictionary * customHeaders = [NSMutableDictionary dictionary];
for (int j = 0; j < arraySize; j=j+2)
{
NSLog (@"Element = %@", [randomSelection objectAtIndex: j]);
NSLog (@"Element = %@", [randomSelection objectAtIndex: j+1]);
NSString * setVal = [randomSelection objectAtIndex: j];
NSString * setHead = [randomSelection objectAtIndex: j+1];
[customHeaders setObject:setVal forKey:setHead]
}

[theRequest setAllHTTPHeaderFields:customHeaders];

关于ios - 来自 NSMutablearray 的 setheader 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594707/

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