1

I have a link which contains the a json file. I mean if I launch that link in chrome a file is downloaded on my computer which is of .json extension. say the link is www.foffa.com/sampleList.json I am new to AFNetworking and not sure how to parse this json, with or without having to write this file down to the disk.

My code looks like below and I am pretty sure that I have to use streams and all for this but I am not sure how. As of now I get an error "Request failed: unacceptable content-type: text/plain" I guess it expects the content type to be "Content-Type" = "application/octet-stream";

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFHTTPResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/octet-stream"];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"Data retrived");       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];
1
  • Content type of your response seems to be set to text/plain. Either you need to get it changed from your server side to the one you are accepting or you need to add text/plain as acceptable content types. Commented Jul 27, 2015 at 10:29

2 Answers 2

2

Change the second line in this:

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

to this:

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

Note you are using the AFJSONResponseSerializer response serialised.

Update

Updating my answer from info in the below comments, it appears you actually want to download a JSON file from a server and then parse it as opposed to parsing JSON directly from the HTTP response:

In order to do that change the content type to text/plain and with the downloaded file/data parse either as a String or a JSON object like this:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:nil];
operation.responseSerializer = [AFHTTPResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes =
    [NSSet setWithObjects:@"application/octet-stream", @"text/plain", nil];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSError *error;
  id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];
  if (error) {
      NSLog(@"Error: %@", error);
  } else {
      NSLog(@"JSON: %@", json);
  }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                      message:[error localizedDescription]
                                                     delegate:nil
                                            cancelButtonTitle:@"Ok"
                                            otherButtonTitles:nil];
  [alertView show];
}];
Sign up to request clarification or add additional context in comments.

7 Comments

Yes... I think the idea here is to download the file first and then parse it. May be afnetworking can't do it on the fly
I couldn't get that url to work when testing (404), so I tested with http://ip.jsontest.com and the above works but you need to change the expected type to application/json not application/octet-stream I would test with that http://ip.jsontest.com or some other valid JSON response vs the one you had as it seems the URL is forcing a download of that file.
can you give this in objective-c please.. I m not familiar with swift.
I was practically doing the same thing except for the @"text/plain" value in acceptable content types.. why do you thing it used to fail..?
@AnkitSrivastava AFNetworking is pretty particular in the response it expects. You were saying that you are expecting a application/octet-stream response but in fact were getting a text/plain response so it was throwing an error saying you are getting the wrong type of data back.
|
0

AFNetwroking won't parse income data for you.

Use NSJSONSerialization's class method +JSONObjectWithData:options:error:. If your data is valid, result will be NSDictionary with same structure as original JSON file

You should place it inside success block

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.