0

我正在尝试使用 AFNetworking 发送 Google Places API 请求。

我有以下代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:OPIA_USERNAME password:OPIA_PASSWORD];
[manager.requestSerializer setValue:API_HEADER_RESPONSE_FORMAT forHTTPHeaderField:@"Accept"];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];

parameters = @{@"key": GOOGLE_API_BROWSER_KEY, @"location": "-27.476383,153.014786", @"radius": [NSString stringWithFormat:@"%i", GOOGLE_PLACES_SEARCH_RADIUS], @"sensor": @"true", @"keyword": type, @"opennow": @"true", @"rankby": @"distance"};
requestURL = [NSString stringWithFormat:@"%@%@", GOOGLE_PLACES_API_URL, API_RESPONSE_FORMAT];
[manager GET:requestURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

它会生成一个这样的 URL:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=XXXX&keyword=coffee&location=-27.476383%2C153.014786&opennow=true&radius=100&rankby=distance&sensor=true

如您所见,位置参数中的“,”是由 AFNetworking 编码的。

我正在使用 AFNetworking 2.0。

4

2 回答 2

1

我这样使用 ut :

NSString* urlString = [NSString stringWithFormat:GOOGLE_PLACES_API_JSON, latitude,longitude, radius, types];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:urlString
      parameters:nil
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             //do stuff
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             //show error
         }];
于 2013-12-18T03:19:23.167 回答
0

我不得不放弃“参数”方法,而是操纵 URL 字符串。

于 2014-02-14T03:02:45.237 回答