gpt4 book ai didi

ios - 将NSData转换为NSString-NSLog限制

转载 作者:行者123 更新时间:2023-12-01 16:02:46 24 4
gpt4 key购买 nike

我正在尝试在Zendesk中将NSData转换为NSString,但是得到的字符串不完整,或者至少无法完全解码data

当我curl以下命令时:

curl https://mySubdomain.zendesk.com/api/v2/users/1038194884/tickets/requested.json   -v -u myLogin:myPassword

我得到正确的响应,它列出了该用户的所有票证,响应如下:

GMT 内容长度:12576 <连接:保持 Activity <
X-Zendesk-API版本:v2 X帧选项:SAMEORIGIN 剩余X速率限制:399 <严格传输安全性:
最大年龄= 31536000; <与X-UA兼容:IE = Edge,chrome = 1 “98b10cfcb2f1577122a1662926f9565c” <缓存控制:必须重新验证,
private ,最大年龄= 0 app2.pod7.fra1.zdsys.com db8b90fd-e3a3-452d-c5ea-ecf4bbd76e89 X机架缓存:丢失 X-Content-Type-Options:nosniff

当我尝试从 objective-C应用程序在 iOS中进行相同操作时,响应几乎相同,如下所示:

“Cache-Control” =“必须重新验证, private ,最大年龄= 0”;
连接=“保持 Activity ”;
“Content-Encoding” = gzip;
“Content-Type” =“应用程序/ json; charset = UTF-8”;
Date =“Sat,28 Jan 2017 20:36:38 GMT”;
Etag =“\” 98b10cfcb2f1577122a1662926f9565c \“”;“;
服务器= nginx;
“Strict-Transport-Security” =“最大年龄= 31536000;”;
“传输编码” =身份;
“X-Content-Type-Options” = nosniff;
“X-Frame-Options” = SAMEORIGIN;
“X-Rack-Cache” =未命中;
“X速率限制” = 400;
“剩余X速率限制” = 399;
“X-Request-Id” =“53c1b462-2dec-4c46-cc60-ecf4bbd76e89”;
“X-Runtime” =“0.209525”;
“X-UA-Compatible” =“IE = Edge,chrome = 1”;
“X-Zendesk-API-Version” = v2;
“X-Zendesk-Application-Version” =“v8.28”;
“X-Zendesk-Origin-Server” =“app4.pod7.fra1.zdsys.com”;
“X-Zendesk-Request-Id” = 675a​​e5b4ff78fbe7

问题是,当我尝试将 data转换为 dictionaryNSString时,我得到了不完整的 string。这是我正在使用的代码:
NSString *urlUser = [NSString stringWithFormat:@"https://mySubdomain.zendesk.com/api/v2/users/1038194884/tickets/requested.json"];
NSURL *url = [[NSURL alloc] initWithString:urlUser];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request addValue : @"application/json"
forHTTPHeaderField : @"Content-Type" ];

NSString *authStr = @"username:password";
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"finished");
if (!error)
{

NSLog(@"%@", data);
NSLog(@"RESPONSE: %@", response);

NSLog(@"length: %lu", data.length);

//Determine if string is null-terminated
char lastByte;
[data getBytes:&lastByte range:NSMakeRange([data length]-1, 1)];

NSString *str;

if (lastByte == 0x0) {
//string is null-terminated
str = [NSString stringWithUTF8String:[data bytes]];
} else {
//string is not null-terminated
str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

NSLog(@"string length: %lu", str.length);

NSLog(@"%@", str);

NSLog(@"stop");




}

}];
[task resume];

顺便说一句, data的长度与 curl响应中的相同。

记录的字符串:

{“tickets”:[{“url”:“ https://mySubdomain.zendesk.com/api/v2/tickets/407.json”,“id”:407,“external_id”:null,“via”:{“channel”:“mobile_sdk”,“source”:{“from”:{ },“to”:{},“rel”:“mobile_sdk”}},“created_at”:“2017-01-26T10:48:28Z”,“updated_at”:“2017-01-26T11:05:42Z” ,“type”:null,“subject”:“Comentário
sobreexercício。“,” raw_subject“:”Comentáriosobre
exercício。“,” description“:”正在使用时:bruna villete \ nObjectId:
qHQzNbjUzy \ nPlano:铁训练-巴西\ n \ n要进行锻炼的细节:
\ nTitulo:CADEIRA EXTENSORA \ nObjectId:qvq5deXxD9 \ nPerfil:MULHER
INICIANTE \nFerequência:5 \nPeríodo:Semana1 \nSérie:意甲
B \ n \nCOMENTÁRIO:
\ nbom”,“优先级”:null,“状态”:“已解决”,“收件人”:null,“requester_id”:1038194884,“submitter_id”:1038194884,“assignee_id”:3030446965,“organization_id”:null,“group_id” “:24989965,” collaborator_ids“:[],” forum_topic_id“:null,” problem_id“:null,” has_incidents“:false,” is_public“:true,” due_at“:null,” tags“:[[seriecomment”] ,“custom_fields”:[],“satisfaction_rating”:null,“sharing_agreement_

最佳答案

正如我在评论中告诉您的,看来NSLog语句切断了字符串。一切都按预期进行。

要处理您的数据,请将其转换为json对象并按以下方式使用它:

// convert the response data to json
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!error) {
NSLog(@"json parsing succeeded.");
}

// access your data
NSArray *tickets = json[@"tickets"];

关于ios - 将NSData转换为NSString-NSLog限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41914815/

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