Reputation: 9157
I am using AFNetworking
, and I am following a tutorial about drawing routes on the iphone screen by getting directions from google directions. I am using JSON
, and AFNetworking
. I copied the code in from the tutorial you can find here: Tutorial
If you also choose to copy and test this code, just a note: You need the AFNetworking
from this github page: AFNetworking Download
You also have to define the variable _path
as an NSMutableArray
in the .h yourself or you will get errors as they have not defined it but referenced it.
Here is the code:
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];
So heres my problem
Thank you to those who helped. I have tested out the code with no errors, but now found that while trying to make a route. It crashes here:
- (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];
}
}
With the error description:
-[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x2004bb80
Can you guys help? Thanks!
Upvotes: 2
Views: 551
Reputation: 7850
Let me also say that the AFHTTPClient previously did not have this HTTPOperationWithRequest class method as shown, but I had to copy and paste it in from AFHTTPRequestOperation.
I cloned AFNetworking
from your github link and found this in AFHTTPClient.h
file:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
I wonder why you are getting errors instead of "method not found" warning. HTTPRequestOperationWithRequest
is instance method, not class method:
AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
// do something
;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
;
}];
By the way, tutorial you linked has it correctly.
Upvotes: 2
Reputation: 9157
Alright after playing a bit I figured it out. The route
would only be an NSDictionary
if JSONKit
was included. Some relation with AFNetworking
and JSONKit
here. So, I already had the JSONKit
files from before, because I knew about this. But AFHTTPClient
was not accepting it, so I had to put the line:
[_httpClient setDefaultHeader:@"Accept" value:@"application/json"];
Upvotes: 0