Eddie K
Eddie K

Reputation: 513

NSURLConnection works, but AFNetworking doesn't (on specific URL)

I set up a simple web server on an AWS EC2 instance and wrote a small test.php file that goes like this:

<?php
$dict = array("key" => "value", "test" => "Hello world");
echo json_encode($dict);
?>

In my browser I can go to http://'myInstanceIP'/test.php and I will get the encoded JSON response. I tried doing this in iOS and for some reason I can only get a successful response via NSURLConnection and not through AFNetworking. It's weird because the AFN code works if I input some other URL that will give me a JSON response, just not on my URL.

I call it in viewDidAppear just for testing:

- (void)viewDidAppear:(BOOL)animated
{
    NSString *urlString = @"http://54.183.178.170/test.php";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"%@", (NSDictionary *)responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failed");
    }];
    [operation start];
}

I can't tell if it's the fault of my web server or AFN (since it works on NSURLConnection).

UPDATE: Here's the failure code I'm getting from AFNetworking:

2014-09-02 11:50:11.538 testingAWS[26751:60b] Failed, error: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8c4ce00 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x8d80fa0> { URL: http://54.183.178.170/test.php } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 34;
    "Content-Type" = "text/html";
    Date = "Tue, 02 Sep 2014 18:50:09 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.4.7 (Ubuntu)";
    "X-Powered-By" = "PHP/5.5.9-1ubuntu4.3";
} }, NSErrorFailingURLKey=http://54.183.178.170/test.php, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, com.alamofire.serialization.response.error.data=<7b226b65 79223a22 76616522 2c227465 7374223a 2248656c 6c6f2077 6f726c64 227d>}

Upvotes: 2

Views: 5234

Answers (2)

Eddie K
Eddie K

Reputation: 513

So it turns out I need to add the following line to my PHP, right before json_encode():

    header('Content-type: application/json');

Then it works with AFNetworking as it should.

Upvotes: 5

jMelnik
jMelnik

Reputation: 1055

We can see here:

Code=-1016 "Request failed: unacceptable content-type: text/html"

That your problem should be happening because of this line:

operation.responseSerializer = [AFJSONResponseSerializer serializer];

For some reason your server must be returning a non-JSON response, and you are using a JSONResponseSerializer. So if you fix the server return type the correct format you'll probably be fine.

Or, you could just change the serializer type like this:

operation.responseSerializer = [AFHTTPResponseSerializer serializer];

if it's just for a testing purpose.

Upvotes: 8

Related Questions