【问题标题】:AFNetwork and GoogleAPI returning NULLAFNetwork 和 GoogleAPI 返回 NULL
【发布时间】:2012-12-07 08:54:02
【问题描述】:

在对 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) from no error { “缓存控制”=“公共,最大年龄=86400”; “内容编码”= gzip; “内容长度”= 1603; “内容类型”=“应用程序/json;字符集=UTF-8”; 日期 =“格林威治标准时间 2012 年 12 月 7 日星期五 08:51:58”; 过期 =“星期六,2012 年 12 月 8 日 08:51:58 GMT”; 服务器 = mafe; Vary = "接受语言"; “X-Frame-Options”=SAMEORIGIN; “X-XSS-保护”=“1;模式=块”; } http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade

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

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

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

  • 滑动传感器布尔值真/假。
  • 将用户代理伪装成常规的 safari。
  • 使用 POST 而不是 GET。

没有运气......

【问题讨论】:

  • 它对我来说很好用 :),你检查的是哪个 iOS 版本?
  • 我正在检查 6.0 和 6.0.1

标签: iphone objective-c google-api geocoding afnetworking


【解决方案1】:

如果问题仍然存在,我建议改用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]);
               }];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-26
    • 2021-04-11
    • 2015-09-08
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多