Search code examples
iosjsonios6httprequestafnetworking

http request from iOS


I am trying to send a request to

"http://www.geocodefarm.com/api/forward/json/f0e3d8f953e83ca08c1eb487c64f9b3ecc379d5c/530 W Main St Anoka MN 55303 US/"
which returns a JSON.

Pasting the address in my browser returns a JSON, I just don't know how to do it in iOS

I tried the following in AFNetworking, but the return was:

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x14930080 {
NSUnderlyingError=0x14753ac0 "bad URL", NSLocalizedDescription=bad URL}

code:

    url = [NSURL URLWithString:"http://www.geocodefarm.com/api/forward/json/f0e3d8f953e83ca08c1eb487c64f9b3ecc379d5c/530 W Main St Anoka MN 55303 US/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                                 initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation , id responseObject) {
    NSLog(@"%@",responseObject);
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error);
                // code
            }];
[operation start];

Thanks


Solution

  • Your problem most likely relies on your URL. All URLs used in NSURLRequest should be escaped. Try to escape your URL before using it:

    NSString* yourURLString = @"http://www.geocodefarm.com/api/forward/json/f0e3d8f953e83ca08c1eb487c64f9b3ecc379d5c/530 W Main St Anoka MN 55303 US/";
    NSString* escapedUrlString =[yourURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    url = [NSURL URLWithString:escapedUrlString];