所以我正在发送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>
之间的响应放入字典或数组中?
答案 0 :(得分:0)
您有2个选择。 1.使用NSString的API(例如“ componentsSeparatedByString:”)生成NSArray。参数应在和之间的子字符串中定义。 2.使用NSObject + YYModel。 https://github.com/ibireme/YYModel