gpt4 book ai didi

php - 将 HTTP POST JSON 参数发送到 PHP 页面 - iOS

转载 作者:行者123 更新时间:2023-12-01 16:43:34 25 4
gpt4 key购买 nike

所以我试图通过 POST 将参数发送到 php 服务器,并且该参数值必须是 JSON 值。参数的名称必须是“数据”

我的意思是,使用 get 方法类似于 http:url.com/url/something.php?data={"jsonkey1":1,"jsonkey2":[.... 或类似的东西

这是我现在拥有的代码,但我无法让它工作。我不喜欢使用外部库,而是使用本地方法。

// Writing JSON...
NSNumber *imagesCount = [[NSNumber alloc] initWithInt:0];
NSMutableArray *arrayList = [[NSMutableArray alloc] init]; // That's an empty array
//[arrayList addObject:@"image1.png"]; [arrayList addObject:@"image2.png"];
NSDictionary *dictionaryJSON = [NSDictionary dictionaryWithObjectsAndKeys:imagesCount, @"count",arrayList,@"list", nil];

NSString *param = @"data=";
NSMutableData *dataJSON = [[NSMutableData alloc]init];
[dataJSON appendData:[param dataUsingEncoding:NSUTF8StringEncoding]];

NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryJSON options:NSJSONWritingPrettyPrinted error:&error];;
[dataJSON appendData:jsonData];

NSString* jsonString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@, %@", jsonString, error);

jsonString = [jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Request to server
NSString *url =[NSString stringWithFormat:@"http://url.com/ios/api/get_images_cache.php?"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"%i", [dataJSON length]] forHTTPHeaderField:@"Content-Length"];


NSString* jsonStringdat = [[NSString alloc] initWithBytes:[dataJSON bytes] length:[dataJSON length] encoding:NSUTF8StringEncoding];
jsonStringdat=[jsonStringdat stringByReplacingOccurrencesOfString:@" " withString:@""];
jsonStringdat=[jsonStringdat stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSLog(@"Final jsonData as string:\n%@, %@", jsonStringdat, error);
//[request setValue:jsonStringdat forHTTPHeaderField:@"data"];
//[request setValue:jsonData forKey:@"data"];
[request setHTTPBody: [jsonStringdat dataUsingEncoding:NSUTF8StringEncoding]];

谢谢你的帮忙。

编辑:

我终于做到了,问题出在这两行:
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

我删除了它们,最后我让它工作了,谢谢!

最佳答案

NSURLConnection* connection = [NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

将其添加到最后,然后您需要使您的类符合 ,然后它可以知道请求何时成功完成以及何时失败并出现错误等。此外,我在我的应用程序中构建请求的方式是:
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setTimeoutInterval:10];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postData];

我希望这会有所帮助!

编辑:
此外,在发送之前将您拥有的 JSON 转换为字符串。我是这样做的:
NSData *dataFromJSON = [NSJSONSerialization dataWithJSONObject:jsonObjects options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:dataFromJSON encoding:NSUTF8StringEncoding];

关于php - 将 HTTP POST JSON 参数发送到 PHP 页面 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013320/

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