I am using PromiseKit and I want to retrieve a JSON file.
[NSURLConnection GET:#"http://127.0.0.1/pack01.json"].then(^(NSDictionary *json) {
NSArray *questions = json[#"questions"];
Everything is fine on localhost but when I try it from another source (same file) it gives me an error:
[NSURLConnection GET:#"https://dl.dropboxusercontent.com/u/11377305/resources/pack01.json"].then(^(NSDictionary *json) {
NSArray *questions = json[#"questions"];
Error
[__NSCFData objectForKeyedSubscript:]: unrecognized selector sent to instance 0xb07c880
JSON file:
https://dl.dropboxusercontent.com/u/11377305/resources/pack01.json
Why is it working on localhost?
I would appreciate any suggestions or even any thoughts on what questions I should be asking.
The error indicates that you're being passed an NSData rather than an NSDictionary from the remote URL.
The PromiseKit documentation states:
PromiseKit reads the response headers and decodes the result you actually wanted (in a background thread):
Therefore it's a safe bet that your remote server (which is DropBox in this case) isn't indicating that the returned data is formatted as JSON. So PromiseKit isn't parsing it.
EDIT: confirmed. DropBox is returning:
Content-Type text/plain; charset=utf-8
So PromiseKit has no way of knowing that the included data is meant to be JSON. You need a server that will return an application/json Content-Type. Or else you can force the JSON parse yourself, but then you're missing out on a large part of the PromiseKit benefit as you end up writing the same boilerplate code as the rest of us.
Related
I have a program that need to call a 3rd party web service to get the JSON data.
However, I get a nil responseObject in success block when the response data is around 1.7 MB plain JSON text using the function [AFHTTPRequestOperationManager GET:parameters:success:failure:]
The function is OK if the response data is in smaller chunk.
Is there any text length limit for the iOS JSON framework to parse it?
How should I deal with this problem unless asking the 3rd party to output the data by chunk?
it may be problem slow network . so set the timeout of the request in Your AFHTTPRequestOperationManager class. and Default timeout of request is 60.
[request setTimeoutInterval:100];
I have an iOS app using Restkit. Most of the response bodies from the server are in XML format. However, there is a few API's that will only send a response like "Success" in text/plain format. When calling these API's I get a 200 response but restkit will throw a mapping error because it is expecting Content-Type = application/xml as is seen in the error below.
NSLocalizedDescription=Loaded an unprocessable response (200) with content type 'application/xml'} response.body=success
I am using the RKXMLReaderSerialization class to interpret the XML received from the server and this is how I register it.
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:#"application/xml"];
It appears from the documentation you can register multiple MIMETypes but I haven't been able to figure out how. If that is possible can someone point it out to me. If not is there a way to handle this?
UPDATE/SOLUTION
After further research and help from Wain I discovered the error above was actually stating that the response-header for content-type had a value of application/xml. This keyed restkit to try and map the response.body as an application/xml formatted body. With a response.body containing only the word "success" the response was unprocessable. I ended up pointing this out to my server guy and he corrected the response to be formatted in the MIMEType declared by the Accept header sent with the request.
You can call setAcceptHeaderWithMIMEType: on your RKObjectManager to tell it what mime types are acceptable in the response. You may need to define and register your own serialisation class too so that RestKit can execute the whole mapping workflow without errors.
I have an iOS app that is using RestKit 0.20.1 to pull data from a server. The server at this time can send JSON formatted data but is not able to receive JSON formatted data.
This is where my problem is. POST requests require HTTP Body and the server is set up to receive XML. I have implemented the RKXMLReaderSerialization add on so I can receive XML but I can't find any current way of sending an XML formatted HTTP Body with RestKit.
This question "Send post request in XML format using RestKit " is what I was looking for but the answer from Imran Raheem is now (as far as I can tell) obsolete due to changes in RestKit.
I am using this method for the POST
[[RKObjectManager sharedManager] postObject:nil path:#"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];
Here is what the RestKit objectManager says about the postObject method
/**
Creates an `RKObjectRequestOperation` with a `POST` request for the given object, and enqueues it to the manager's operation queue.
#param object The object with which to construct the object request operation. If `nil`, then the path must be provided.
#param path The path to be appended to the HTTP client's base URL and used as the request URL. If nil, the request URL will be obtained by consulting the router for a route registered for the given object's class and the `RKRequestMethodPOST` method.
#param parameters The parameters to be reverse merged with the parameterization of the given object and set as the request body.
#param success A block object to be executed when the object request operation finishes successfully. This block has no return value and takes two arguments: the created object request operation and the `RKMappingResult` object created by object mapping the response data of request.
#param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data. This block has no return value and takes two arguments:, the created request operation and the `NSError` object describing the network or parsing error that occurred.
If I have the MIMEType set to JSON the Trace shows my request.body is being populated like so request.body={"Search":"Trending"}.
However if I set the MIMEType to XML the Trace shows the request.body=(null)
Here is the line I use to change the MIMEType
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
I am pretty new to iOS and Objective-C so I may be setting up the NSDictionary that is used in the parameters of the `postObject' method wrong. Here it is just in case....
NSArray *objects =[NSArray arrayWithObjects:#"trending", nil];
NSArray *keys =[NSArray arrayWithObjects: #"Search",nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
self.search=params;
Any help would be greatly appreciated! Given that I am new Snippets are especially helpful!
Oh and BTW if anyone can point me to REST method that accepts JSON input I would gladly pass it on to my server guy so I can just avoid XML all together.
As feared and suggested in other posts on this topic writing XML is not supported in version of RestKit 0.20.1. Here is a link to my conversation with Blake Watters on the subject.
https://github.com/RestKit/RestKit/issues/1430#issuecomment-19150316
I never did clear up why the request.body= (null) when the MIMEType is set to XML.
The manager of the server has agreed to set it up to receive JSON. That is how I plan to work around this.
I have a method for downloading a file as file_name.zip to the documents directory. However, if I receive an error, such as 'file does not exist', the library is writing the JSON payload to file_name.zip.
How do we check for JSON error in requestFinished:?
responseString or responseData is always null.
You probably want to check the request.responseStatusCode and check against a 404 error. But honestly you should look into AFNetworking. ASIHTTPRequest is no longer supported and AFNetworking has success/failure blocks.
I'm using RestKit for HTTP requests, and with some request, I get JSON response with some busses:
[{"bus_number":"1","created_at":"2011-08-15T23:07:52Z","id":1,"model":"Setra","registar_number":"123456","seats":50,"tour_id":1,"updated_at":"2011-08-15T23:07:52Z"},{"bus_number":"2","created_at":"2011-08-15T23:07:52Z","id":2,"model":"Mercedes","registar_number":"2234","seats":60,"tour_id":1,"updated_at":"2011-08-15T23:07:52Z"}]
I'm getting it into method:
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(#"%#", [response bodyAsString]);
}
Becouse, I have multiple busses in this JSON, how can I easily parse/decode or whathever do to convert this into NSArray or something for easily accesing into elements?
EDIT: I need code sample how to do this with JSONKit, who is already implemented in RESTKit.
json-framework is one of many third-party libraries you can use to parse JSON.