ios - Dealing with unexpected response content types with AFNetworking / AFHTTPRequestOperation -
I am using AFNetworking to process HTTP requests in my iOS app. I went into a stumbling I can not be sure what the reaction content will be like, but before processing it will have to set up the response serializer. This means that I can request an API, I can expect an image, but there is actually some authentication error, so the server instead returns a JSON-formatted response.
Here is my code:
AFHTTPRequestOperation * op = [[AFHTTPRequestOperation alloc] initWithRequest: request]; [OPP Set Response Laser: [AFJSNRPpsCensializer Serializer]]; // ??? [op setcompletionBlockWithSuccess: ^ (AFHTTPRequestOperation * Operations, ID response object) {NSDictionary * Result = (NSDictionary *) responseObject; OnSuccess ((NSArray *) results); } Failure: ^ (FHttpOrQuest Operation * Operation, NSERR * Error) {Onfiler (Error); }]; [Op start]; As you can see, I had to set the expected content type contained by setting up the responsiveness serializer to [AFJSONRPSPenceSyriaLizer Serializer]. So if I get something else, then this causes an error, even though I still want to parse a process that responds when reacting with error.
So my question is, should I just use the standard AFHTTPResponseSerializer, check the response status code and then manually process the reaction body (as json, xml, html image etc.)?
On the serializer, you want to set the approved content types:
< code> FGSNransponseSerialiser * Serializer = [AFJSONRPSPenceSyriaLizer Serializer]; Serializer.acceptableContentTypes = [NSSet set defers: @ [@ "text / plain", @ "text / html"]]; [Opset Set Scriiler: Serializer]; By: From:
By default, the following MIME type accepts, in which the official standard, application / JSN, as well as other commonly used Type:
application / json text / json You do not have to use AFJSONResponseSerializer You can create your own serializer, as long as it complies with the protocol. If you have JSON responses but the XML error responses, then you can only sub-class AFHTTPResponseSerializer Do processing. You can also use different paragraph types to parse, which you are going through the serializer giving it.
Comments
Post a Comment