【问题标题】:Obj-C Taking server response for parameter and putting it into an arrayObj-C 将服务器响应作为参数并将其放入数组中
【发布时间】:2019-09-06 13:42:55
【问题描述】:

所以我正在发送一个 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' 我应该怎么做才能将 &lt;photoURL&gt; 之间的响应放入字典或数组中?

【问题讨论】:

  • 我发现了如何做到这一点,最好的方法是使用 XML 解析器,如果有人看到这个
  • 回答了我自己的问题

标签: ios objective-c uitableview uiimageview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2013-02-23
    • 2018-10-05
    • 2013-07-11
    • 2012-12-16
    • 1970-01-01
    • 2021-04-28
    • 2016-01-22
    相关资源
    最近更新 更多