2

我正在使用AFNetworking,并且我正在通过从谷歌方向获取方向来关注关于在 iphone 屏幕上绘制路线的教程。我正在使用JSON, 和AFNetworking. 我从教程中复制了代码,您可以在此处找到:教程

如果您还选择复制和测试此代码,请注意:您需要AFNetworking来自此 github 页面:AFNetworking 下载

您还必须自己在 .h 中将变量定义_path为 an NSMutableArray,否则您将收到错误,因为他们没有定义它但引用了它。

这是代码:

        AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];

    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:@"origin"];

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:@"destination"];

    [parameters setObject:@"true" forKey:@"sensor"];

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];

    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

    AFHTTPRequestOperation *operation = [AFHTTPClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response) { 
        NSInteger statusCode = operation.response.statusCode;
        if (statusCode == 200) {
            [self parseResponse:response];

        } else {

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; 

    [_httpClient enqueueHTTPRequestOperation:operation];

所以这是我的问题

感谢那些帮助过的人。我已经测试了没有错误的代码,但现在在尝试制作路线时发现了这一点。它在这里崩溃:

- (void)parseResponse:(NSDictionary *)response {

    NSArray *routes = [response objectForKey:@"routes"]; // CRASH HERE

    NSDictionary *routePath = [routes lastObject];

    if (routePath) {

        NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"];

        _path = [self decodePolyLine:overviewPolyline];

        NSInteger numberOfSteps = _path.count;

        CLLocationCoordinate2D coordinates[numberOfSteps];
        for (NSInteger index = 0; index < numberOfSteps; index++) {
            CLLocation *location = [_path objectAtIndex:index];
            CLLocationCoordinate2D coordinate = location.coordinate;

            coordinates[index] = coordinate;
        }

        MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
        [self.mapView addOverlay:polyLine];

        }

    }

带有错误描述:

-[__NSCFData objectForKey:]:无法识别的选择器发送到实例 0x2004bb80

你们能帮忙吗?谢谢!

4

2 回答 2

2

我还要说,AFHTTPClient 之前没有如图所示的这个 HTTPOperationWithRequest 类方法,但我不得不从 AFHTTPRequestOperation 复制并粘贴它。

AFNetworking我从您的 github 链接克隆并在AFHTTPClient.h文件中找到了这个:

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request 
                                                    success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                                                    failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

我想知道为什么您收到错误而不是“找不到方法”警告。HTTPRequestOperationWithRequest是实例方法,而不是类方法:

AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // do something
        ;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        ;
    }];

顺便说一句,您链接的教程正确无误。

于 2012-08-06T10:23:37.227 回答
0

好吧,玩了一会儿我就明白了。这route将只是一个NSDictionaryifJSONKit被包括在内。AFNetworkingJSONKit这里有一些关系。所以,我已经有了以前的JSONKit文件,因为我知道这一点。但AFHTTPClient不接受它,所以我不得不说:

        [_httpClient setDefaultHeader:@"Accept" value:@"application/json"];
于 2012-08-07T00:12:09.777 回答