AFNetworking 2.0 with large JSON response - ios

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];

Related

URLRequest fewer characters

I'm trying to fetch some weather data (Location forecast). This request return a couple of kb. OK, so I would only like to request the first part or terminate the request when i get the line
<temperature id="TTT" unit="celsius" value="xxxx"/>
Is it possible to do a URLRequest in iOS Swift that request a certain number of bytes/chars. Or is put into chunks and then terminated when temperature is received. I could setup a server acting as proxy and cut down the data exchanged to my app, but it would be nice if I could avoid this.

NSURLConnection retrieving JSON

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.

What is Mediafire expecting on upload request

I'm attempting to upload a file using Mediafire's API. Its not clear to me what they expect in the body of the message. I'm attempting to follow the API described in https://www.mediafire.com/developers/upload.php#upload
My understanding is that some parameters are passed in a query string as part of the URL. I'm passing the session_token on the URL.
I set an HTTP header for the file size, x-filesize.
I'm setting the method to POST and sending to base url (before the query string) http://www.mediafire.com/api/upload/upload.php
Its not clear to me what should be in the body. I tried including the pure data from the file being uploaded. I've also tried adding more data to make it look like multipart form.
In either case I'm getting no response at all from the server. I'm doing this in objective-c on a Mac. The NSURLConnection request comes back with nil response and nil error. I'm using
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
The Mediafire upload API is expecting parameters to be send in the URL as the query string, GET style.
The data from the file is included in the body as a multipart form. If the body is improperly constructed, Mediafire may not return a relevant error.
Headers specified in the API are sent as headers in the body.
The 'path' parameter only appears to be honored if a uploadkey is also provided. No error is provided indicating if the path parameter was honored.
Maximum file size appears to be around 4MB.

How to send a XML formatted POST request with RestKit 0.20.1

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.

Streaming Results from Mochiweb

I have written a web-service using Erlang and Mochiweb. The web service returns a lot of results and takes some time to finish the computation.
I'd like to return results as soon as the program finds it, instead of returning them when it found them all.
edit:
i found that i can use a chunked request to stream result, but seems that i can't find a way to close the connection. so any idea on how to close a mochiweb request?
To stream data of yet unknown size with HTTP 1.1 you can use HTPP chunked transfer encoding. In this encoding each chunk of data prepended by its size in hexadecimal. Last chunk is a zero-length chunk, with the chunk size coded as 0, but without any data.
If client doesn't support HTTP 1.1 server can send data as binary chunks and close connection at the end of the stream.
In MochiWeb it's all works as following:
HTTP response should be started with Response = Request:respond({Code, ResponseHeaders, chunked}) function. (By the way, look at the code comments);
Then chunks can be send to client with Response:write_chunk(Data) function. To indicate client the end of the stream chunk of zero length should be sent: Response:write_chunk(<<>>).
When handling of current request is over MochiWeb decides should connection be closed or can be reused by HTTP persistent connection.

Resources