I’m having an odd issue here, and surprised I haven’t found anyone else with the same problem.
I’m using AFNetworking to make a AFJSONRequestOperation.
It works the first time a network connection is made. However, the same code fails once a network connection is made and displays a ‘Bad URL’ error.
The weird part is, the app never even pings the server before failing, I’m using Charles to sniff all requests.
Has anyone else experienced this?
For reference, here is the code:
NSURL *url = [NSURL URLWithString:JOIN_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// httpClient.parameterEncoding = AFJSONParameterEncoding;
NSString *path = [NSString stringWithFormat:@"%@?%@",JOIN_URL, getString];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:path parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"SUCCESS JSON: %@", JSON);
NSLog(@"RESPONSE URL: %@",response.URL);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"FAIL JSON: %@", JSON);
NSLog(@"FAIL ERROR: %@", error.description);
NSLog(@"RESPONSE URL: %@",response.URL);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" message:@"Cannot connect now, please try again" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
[alert show];
}];
[operation start];
As far as I understand
AFHTTPClient, you provide it with abaseURLwhich is the Base URL to which all path you specify will be appended. And then when you provide a path, you only provide the relative part of this path.So if you have a WebService at http://www.example.com/webservice/ which has some methods like
/listAll?n=10for example, you will only provide"listAll"to thepathargument ofrequestWithMethod:path:parameters:and a dictionary@{ @"n" : @10 }to theparametersargument.You already provided your
JOIN_URLwhen you instanciated yourAFHTTPClientanyway, so if you pass thatJOIN_URLagain in the path, it will appear twice in the URL built by theAFHTTPClientinternally!