【发布时间】: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' 我应该怎么做才能将 <photoURL> 之间的响应放入字典或数组中?
【问题讨论】:
-
我发现了如何做到这一点,最好的方法是使用 XML 解析器,如果有人看到这个
-
回答了我自己的问题
标签: ios objective-c uitableview uiimageview