AFNetwork and GoogleAPI returning NULL



在对CLGeocoder感到非常失望之后,我决定改用GoogleMaps API。

我使用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];

我得到这样的反馈:

从无错误中获得结果:"(null)"(null{"缓存控制"="public,最大年龄=86400";"内容编码"=gzip;"内容长度"=1603;"内容类型"="application/json;charset=UTF-8";日期="2012年12月7日星期五08:51:58 GMT";Expires="2012年12月8日星期六08:51:58 GMT";服务器=mafe;Vary="接受语言";"X-Frame-Options"=SAMEORIGIN;"X-XSS-Protection"="1;mode=block";}http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=胸廓

结果为空,但没有错误。标头中的内容被识别为JSON,但原始JSON为null。

令人讨厌的是,如果我打开http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade在浏览器中,我得到了很多结果。

到目前为止,我已经尝试过:

  • 闪烁传感器booleon true/false
  • 将用户代理伪造为常规safari
  • 使用POST而不是GET

运气不好。。。

如果问题仍然存在,我建议使用MKNetworkKit而不是

这是我的解决方案-

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

代码中的某个位置

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

相关内容

  • 没有找到相关文章

最新更新