ios - NSMutableURLRequest 的响应不包含信息

标签 ios objective-c nsurlsession

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];

NSLog信息是这样的:

2017-02-08 12:17:21.324 XXX[4168:197023] <NSHTTPURLResponse: 0x608000436240> { URL: http://apis.haoservice.com/weather?cityname=%E6%88%90%E9%83%BD&key=af4587a7d87740ae9a0d73697e0cccc9 } { status code: 200, headers {
"Cache-Control" = private;
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 08 Feb 2017 03:48:39 GMT";
Server = "nginx/1.10.2";
"Transfer-Encoding" = Identity;
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
"X-Safe-Firewall" = "zhuji.360.cn 1.0.9.47 F1W1";
} }

但是如果我将此网址发布到浏览器:

http://apis.haoservice.com/weather?cityname=成都&key=af4587a7d87740ae9a0d73697e0cccc9

页面会显示我想要得到的结果:

enter image description here

为什么我的回复没有包含图片信息?

最佳答案

您需要访问服务器响应中的data 以获得JSON 响应,因此使用NSJSONSerialization 来获取Dictionary来自响应数据的对象。

if (error) {
    NSLog(@"%@", error);
} else {
    NSError* parseError;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:&parseError];
    NSLog(@"%@",json);                                   
}

关于ios - NSMutableURLRequest 的响应不包含信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42104651/

相关文章:

ios - 我可以更改 NSLayoutConstraint 的乘数属性吗?

objective-c - NSMutableArray removeObjectAtIndex :0 not behaving as expected

iOS NSURLSessionUploadTask 响应数据

ios - NSURLSessionDataTask dataTaskWithURL 完成处理程序未被调用

Android 录制的 aac(.mp3) 文件无法在 iOS 中播放

ios - 如何在提交到 App Store 之前检测私有(private) api 的使用情况以避免拒绝

ios - 在 iOS 应用程序中集成 CCAvenue 时出现问题

ios - 像UItabbar一样完全透明和 float

ios - 在 sprite kit 中使用渐变效果将 fps 从 60 降低到 30

ios - 将 NSData 转换为 NSDictionary