AFNetwork and GoogleAPI return NULL

After the very disappointment of CLGeocoder, I decided to use the GoogleMaps API instead.

I designed the call as follows: AFNetwork:

AFHTTPClient *new = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]]; NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"thorsgade",@"true", nil] forKeys:[NSArray arrayWithObjects:@"address",@"sensor", nil]]; NSMutableURLRequest *req = [new requestWithMethod:@"GET" path:@"maps/api/geocode/json" parameters:dict]; AFJSONRequestOperation *call = [AFJSONRequestOperation JSONRequestOperationWithRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSArray *geos = [JSON objectForKey:@"results"]; DLog(@"Got result : '%@' %@ from %@ %@ %@",JSON,geos,[NSHTTPURLResponse localizedStringForStatusCode:response.statusCode],response.allHeaderFields,request.URL.description); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { DLog(@"Failed %@ %@",error.localizedDescription,request.URL.description); }]; [call start]; 

I get this feedback:

Got the result: '(null)' (null) due to the error {"Cache-Control" = "public, max-age = 86400"; "Content-Encoding" = gzip; "Content-Length" = 1603; "Content-Type" = "application / json; charset = UTF-8"; Date = "Fri, Dec 07, 2012 08:51:58 GMT"; Expires = "Sat, Dec 08, 2012 08:51:58 GMT"; Server = mafe; Vary = "Accept-Language"; "X-Frame-Options" = SAMEORIGIN; "X-XSS-Protection" = "1; mode = block"; } http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade

Zero result, but no errors. The content is recognized in the headers as JSON, but the source JSON is null.

It’s annoying that if I open http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade in the browser, I get a lot of results.

So far I have tried:

  • Sensor flag booleon true / false.
  • User agent fake for regular safari.
  • Use POST instead of GET.

Bad luck...

+4
source share
1 answer

If the problem persists , I would recommend MKNetworkKit instead

Here is my solution -

GoogleGeocodeApi.h

 //GoogleGeocodeApi.h #import <Foundation/Foundation.h> #import "MKNetworkEngine.h" typedef void (^JsonResponseBlock)(NSDictionary *); typedef void (^ErrorBlock)(NSError* error); @interface GoogleGeocodeApi : MKNetworkEngine -(MKNetworkOperation*) geocodeWithAddress: (NSString *) address onCompletion:(JsonResponseBlock) completionBlock onError:(ErrorBlock) errorBlock; @end 

GoogleGeocodeApi.m

 //GoogleGeocodeApi.m #import "GoogleGeocodeApi.h" @implementation GoogleGeocodeApi -(id)init { if (self = [super initWithHostName:@"maps.googleapis.com" apiPath:@"maps/api/geocode" customHeaderFields:nil]) { } return self; } -(MKNetworkOperation*) geocodeWithAddress: (NSString *) address onCompletion:(JsonResponseBlock) completionBlock onError:(ErrorBlock) errorBlock; { MKNetworkOperation *op = [self operationWithPath:[NSString stringWithFormat:@"json?sensor=true&address=%@", address] params:nil httpMethod:@"GET"]; [op onCompletion:^(MKNetworkOperation *completedOperation) { NSDictionary *responseJSON = [completedOperation responseJSON]; if (responseJSON && [[responseJSON objectForKey:@"status"] isEqualToString:@"OK"]) { completionBlock(responseJSON); } else { NSDictionary* errorDictionary = @{NSLocalizedDescriptionKey :@"Google geocode failed!"}; NSError *error = [NSError errorWithDomain:@"Failed response" code:100 userInfo:errorDictionary]; errorBlock(error); } } onError:^(NSError* error) { errorBlock(error); }]; [self enqueueOperation:op]; return op; } 

Somewhere in the code

 GoogleGeocodeApi *gma = [[GoogleGeocodeApi alloc] init]; [gma geocodeWithAddress:@"thorsgade" onCompletion:^(NSDictionary *responseJSON) { NSLog(@"Geocode succeeded: %@", responseJSON); } onError:^(NSError *error) { NSLog(@"Geocode failed with error: %@", [error localizedDescription]); }]; 
+1
source

All Articles