Ios NSMutableURLRequest';s的响应不包含该信息
NSLog信息如下所示:Ios NSMutableURLRequest';s的响应不包含该信息,ios,objective-c,nsurlsession,Ios,Objective C,Nsurlsession,NSLog信息如下所示: NSString *url = [NSString stringWithFormat: @"http://apis.haoservice.com/weather?cityname=%@&key=%@", cityname, China_Weather_APP_KEY]; NSString *url_after = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet UR
NSString *url = [NSString stringWithFormat: @"http://apis.haoservice.com/weather?cityname=%@&key=%@", cityname, China_Weather_APP_KEY];
NSString *url_after = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url_after]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"%@", response); // Here I print the response information
}
}];
[dataTask resume];
页面将显示我想要得到的结果:
为什么我的响应没有包含图片的信息?您需要访问服务器响应中的
数据
,以获取JSON
响应,因此使用NSJSONSerialization
从rsponse数据获取Dictionary对象
http://apis.haoservice.com/weather?cityname=成都&key=af4587a7d87740ae9a0d73697e0cccc9
http://apis.haoservice.com/weather?cityname=成都&key=af4587a7d87740ae9a0d73697e0cccc9
if (error) {
NSLog(@"%@", error);
} else {
NSError* parseError;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:&parseError];
NSLog(@"%@",json);
}