-1

i wanted to get data from server encoded in json to display it, so i use afnetworking library but i get error here is my code

  NSURL * url =[NSURL URLWithString:@"http://software-sultan.com/alaa/image_test.php"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // 3
    array = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];



} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // 4
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
    NSLog(@"%@",error);
}];

// 5
[operation start];

here is the error

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7f920964bff0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f920965cc20> { URL: http://software-sultan.com/alaa/image_test.php } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 126781;
"Content-Type" = "text/html";
Date = "Thu, 29 Jan 2015 19:00:55 GMT";
"Keep-Alive" = "timeout=30";
Server = "Apache/2";
"X-Powered-By" = "PHP/5.3.13";

} }, NSErrorFailingURLKey=http://software-sultan.

user3045517
  • 61
  • 1
  • 8
  • possible duplicate of [Request failed: unacceptable content-type: text/html using AFNetworking 2.0](http://stackoverflow.com/questions/19114623/request-failed-unacceptable-content-type-text-html-using-afnetworking-2-0) – glyuck Jan 29 '15 at 20:23

1 Answers1

0

It looks like you are trying to parse a JSON response with AFJSONResponseSerializer, but the webserver sends text/html as content type. JSON responses are generally served with the Content-Type: application/json header.

Also, if the response was successfully parsed as JSON, the responseObject given as the second parameter of your completion block would already be a valid NSArray containing your data. So there wouldn't need to create it again using NSJSONSerialization.

Nicolas B.
  • 1,318
  • 1
  • 11
  • 20
  • http://software-sultan.com/alaa/image_test.php if you open link you will see data in form json – user3045517 Jan 29 '15 at 19:25
  • Sure, but just because you return data as JSON valid format, doesn't mean the JSON response serializer will be able to parse it. It must be looking for the `Content-Type` before trying to parse the data, resulting in the error you're having. I would try to set the response as "application/json" to see if this allows the serializer to do its work. – Nicolas B. Jan 29 '15 at 19:29
  • Also, maybe removing the `responseSerializer` (by setting it to `nil`) can help AFNetworking getting your response as pure data, that you could parse using `NSJSONSerialization`. – Nicolas B. Jan 29 '15 at 19:35
  • yes when i removed this line it works right. is this the fastest way to get data from server? – user3045517 Jan 29 '15 at 19:50
  • AFNetworking is offering you to parse the data as JSON automatically when using a `AFJSONResponseSerializer` as responseSerializer (and the correct content-type), but in your case, you just chose to do it yourself. This is not to be considered as a "wrong/right" way to do it. The only thing I can see is that AFNetworking would have done the parsing in a separate thread, allowing your main UI to be responsive in the meantime. So yes, this is considered to be the fastest way. – Nicolas B. Jan 29 '15 at 19:54