Reputation: 26886
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];
The NSLog information is this:
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";
} }
But if I post this url to the browser:
http://apis.haoservice.com/weather?cityname=成都&key=af4587a7d87740ae9a0d73697e0cccc9
The page will show the result I want get:
Why my response did not contain the picture's information?
Upvotes: 0
Views: 36
Reputation: 72410
You need to access the data
that you have got with your server response to get the JSON
response, so use NSJSONSerialization
to get Dictionary object from the rsponse data.
if (error) {
NSLog(@"%@", error);
} else {
NSError* parseError;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:&parseError];
NSLog(@"%@",json);
}
Upvotes: 1