gpt4 book ai didi

iOS 5 Json 序列化

转载 作者:行者123 更新时间:2023-12-01 19:09:57 27 4
gpt4 key购买 nike

我已经使用 JSON 序列化来获取 json 响应,在这里我一切都很好,但是当我需要将一些值作为键值对与 URL 一起发布时。我已经这样做了,但没有得到结果。

NSArray *objects = [NSArray arrayWithObjects:@"uname", @"pwd", @"req",nil];
NSArray *keys = [NSArray arrayWithObjects:@"ann", @"ann", @"login", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:keys forKeys:objects];


if ([NSJSONSerialization isValidJSONObject:dict]) {
NSError *error;

result = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

if (error == nil && result != nil) {
// NSLog(@"Success");
}
}

NSURL * url =[NSURL URLWithString:@"URL_address_VALUE/index.php"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[result length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:result];

NSURLResponse *res = nil;
NSError *error = nil;

NSData *ans = [NSURLConnection sendSynchronousRequest:request returningResponse:&res error:&error];

if (error == nil) {

NSString *strData = [[NSString alloc]initWithData:ans encoding:NSUTF8StringEncoding];

NSLog(@"%@",strData);
}

我不知道这里出了什么问题...请伙计们帮助我..

最佳答案

您的代码中有多个错误,请使用我的代码作为引用并将其与您的代码进行比较,您将获得由您完成的错误。
从 Objective-C 的角度来看,以下代码可以正常工作。您的 URL 或服务端存在一些错误。

工作代码:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"ann",@"uname",@"ann",@"pwd",@"login",@"req", nil];
NSLog(@"dict :: %@",dict);
NSError *error2;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error2];
NSString *post = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"postLength :: %@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://exemplarr-itsolutions.com/dbook/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *error3;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error3];
NSString *str = [[NSString alloc] initWithData:POSTReply encoding:NSUTF8StringEncoding];
NSLog(@"str :: %@",str);

关于iOS 5 Json 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17045072/

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