gpt4 book ai didi

ios - Obj-C 获取服务器响应参数并将其放入数组中

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

所以我发送一个http“POST”请求并使用下面的代码获得响应...

static HistoryViewController *sharedInstance;

+(HistoryViewController *) getInstance
{
@synchronized (self) {
if (sharedInstance == nil) {
sharedInstance = [[HistoryViewController alloc] init];
sharedInstance.photo_URL =@"";
return sharedInstance;
}else{
return sharedInstance;
}
}
}


- (void)viewDidLoad {
[super viewDidLoad];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"http://www.MyURL/getPhotos.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *post = [[NSString alloc] initWithFormat:@"parameter=%@", parameter];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString *text =[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
[HistoryViewController getInstance].photo_URL = [jsonDict objectForKey:@"photoURL"];

}
}];
[dataTask resume];

我从服务器的响应看起来像这样......

"<MyURLResponse><photoURL>photoURLexample</photoURL><created>04/12/2019 at 8:58 am EDT</created></MyURLResponse>"

这只是一张照片,但当更多照片上传到景点时,它会返回所有照片。当我尝试从 JSON 字典中获取数据时,它返回“null” 我应该如何将响应放在 <photoURL> 之间放入字典或数组中?

最佳答案

你有两个选择。1. 使用 NSString 的 API 生成 NSArray,例如“componentsSeparatedByString:”。参数应定义在 和 之间的子字符串中;2.使用NSObject+YYModel。 https://github.com/ibireme/YYModel

关于ios - Obj-C 获取服务器响应参数并将其放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55708425/

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