Collecting data from NSURLConnection - ios

I have the worst internet connection atm, so sorry if this has been asked before..
I have an NSURLConnection for getting some json data. Until now it worked perfectly fine to use the delegate method didReceiveData:(NSData*)data to save the received data. I am downloading data from at least seven different pages at the same time. Today, after updati g on of the json-pages to contain more data, the NSData object seemed corrupt. I have recently been told that this delegate does not return the whole data, and thus corrupting my information.
Is there another delegate like the didFinish only it also returns the full complete object? Or do I have to do this myself, like merging two NSData's?
Sorry for stupidity, and grammatical errors are dedicated to iPhone auto-correct.

You must never, ever rely on didReceiveData: returning the full data, because it will break one day. You have to collect your chunks of data in an NSMutableData:
NSMutableData *d = [[NSMutableData alloc] init];
- (void)connection:(NSURLConnection *)c didReceiveData:(NSData *)data
{
[d appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
// 'd' now contains the entire data
}
If it's inconvenient for you, you can avoid using NSURLConnection and use a background thread to grab the data in one piece using:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://web.service/response.json"]];

Related

Objective-C NSURLConnection didReceiveData creating bad JSON in NSData

I'm currently attempting to stream data from Twitter using their streaming API's. I've attached the code below for creating my NSData and appending to it on didReceiveData. For some reason, every time didReceiveData gets a response from Twitter, it's appended on as a new JSON root into the NSData, so when I attempt to parse the NSData into a JSON structure, it blows up.
I couldn't figure out what was going on and posted the JSON into a validator and it noted that there were multiple roots in the JSON. How can I modify the code to continue to append to the existing JSON root? Or is there an easier way to go about deserializing into JSON when there's multiple JSON entries in the NSData?
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
NSLog(#"Did receive response");
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
NSLog(#"Did Receive data");
[_responseData appendData:data];
}
I think what you need is just some extra logic to handle the real-time nature of this. Use your NSMutableData as a container to continue receiving data, but at the end of each batch you should scan the data object for all valid objects, build them, and store them into a different object that holds all the built json objects. In this example lets assume you have this ivar: NSMutableArray *_wholeObjects
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
NSLog(#"Did Receive data");
[_responseData appendData:data];
[self buildWholeObjects]
}
- (void) buildWholeObjects {
NSArray *rootObjects = <#business logic to return one whole JSON object per array element, or return nil if none found#>
if (rootObjects != nil) {
NSUInteger bytesExtracted = 0;
for (rootObject in rootObjects) {
[_wholeObjects addElement:rootObject];
bytesExtracted += rootObject.length;
}
NSData *remainingData = [_responseData subdataWithRange:NSMakeRange(bytesExtracted, _responseData.length - bytesExtracted];
[_responseData setData:remainingData];
}
}
After doing this only access the objects in _wholeObjects, where each element represents a fully valid JSON object that you can deserialize or read in any way you need.
Just for the sake of clarity, lets say the first NSData represents:
{"a":"2"}{"c":"5
When you process it _wholeObjects will have one element representing {"a":"2"}, and _responseData will now be {"c":"5
Then the next stream of data should continue on the object. Lets say the second NSData is:
"}
Now _responseData is {"c":"5"} because we appended the new message onto the remaining old message. We build this one out, and get a second element in _wholeObjects, and _responseData will be empty and ready to receive the next set of data.
Hope that helps some. I think the hard part for you is going to be determining how much of the _responseData is considered a valid JSON object. If they are simple enough you can just count the number of opening {/[ to closing }/] and pull that substring out.
Just to follow up on this topic for anyone dealing with the same thing: I ended up using SBJson which has support for streaming. http://superloopy.io/json-framework/

Multiple JSON requests in iOS

I wish to fetch data for an array of URLs that return JSON data. I am trying the following code:
for (int i =0; i<numberOfDays; i++)
{
NSData *data = [NSData dataWithContentsOfURL:[wordURLs objectAtIndex:i]];
NSLog(#"%#",[wordURLs objectAtIndex: i]);
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
}
'wordURLs' are the array of URLs and in my 'fetchedData:' method, I save the returned JSON data to a plist file.
The issue is that for all number of times that the loop runs, the data is returned for only one/two particular URLs (i.e. say for the urls at indices at 1 and 3, or 1 and 2 etc). I log and see that the URLs are different for each time the 'data' variable is initialized.
What is a better way of doing this?
I have used NSJSONSerialization for parsing JSON.
There are much better ways of doing this. The problem with what you are trying to do is that it is synchronous, which means your app will have to wait for this action to be completed before it can do anything else. I definitely would recommend looking into making this into an asynchronous call by simply using NSURLConnection and NSURLRequests, and setting up delegates for them.
They are relatively simple to set up and manage and will make your app run a million times smoother.
I will post some sample code to do this a little later once I get home.
UPDATE
First, your class that is calling these connections will need to be a delegate for the connections in the interface file, so something like this.
ViewController.h
#interface ViewController: UIViewController <NSURLConnectionDelegate, NSURLConnectionDataDelegate> {
NSMutableData *pageData;
NSURLConnection *pageConnection;
}
Then you will need to create/initialize the necessary variables in you implementation
ViewController.m
-(void) viewDidLoad {
pageData = [[NSMutableData alloc] init];
NSURLRequest *pageRequest= [[NSURLRequest alloc] initWithURL:pageURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:4];
pageConnection = [[NSURLConnection alloc] initWithRequest:pageRequestdelegate:self];
}
Then you also need the delegate functions that will get called as the data is retrieved.
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (connection == pageConnection) {
[pageData appendData:data];
}
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection == pageConnection) {
// Do whatever you need to do with the data
}
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (connection == pageConnection) {
// Do something since the connection failed
}
}
Of course this example only includes one URL being loaded, but you could make it as many as you want. You will of course have to keep track of all of the necessary NSURLConnections so you know where to put the data you received, as well as what actions to take in case of a failure or the connection being completed successfully, but that is not a hard extension from what I have given.
If you see any glaring errors or something does not work, please let me know.

Dealing with more than one NSURLRequest [duplicate]

This question already has answers here:
Managing multiple asynchronous NSURLConnection connections
(13 answers)
Closed 8 years ago.
I have two NSURLRequest objects, connecting to a web service and invoking 2 different services.
The problem is that I have a random results, sometimes the first one is displayed first and sometimes the second NSURLRequest is the first.
NSString *urla=#"http://localhost:8080/stmanagement/management/retrieve_dataA/?match_link=";
NSString *uria = [urla stringByAppendingString:self.lien_match];
NSURL *urlla= [ NSURL URLWithString:uria];
NSURLRequest *requesta =[ NSURLRequest requestWithURL:urlla];
NSString *urlb=#"http://localhost:8080/stmanagement/management/retrieve_dataB/?match_link=";
NSString *urib = [urlb stringByAppendingString:self.lien_match];
NSURL *urllb= [ NSURL URLWithString:urib];
NSURLRequest *requestb =[ NSURLRequest requestWithURL:urllb];
connectiona=[NSURLConnection connectionWithRequest:requesta delegate:self];
connectionb=[NSURLConnection connectionWithRequest:requestb delegate:self];
if (connectiona){
webDataa=[[NSMutableData alloc]init];
}
if (connectionb){
webDatab=[[NSMutableData alloc]init];
}
Is that correct what I'm doing? Should I add a small break between the two NSURLRequests?
Because at every view execution I have a random result. (I'm setting the results to two UITableView objects).
I think your "problem" is that self is the connection delegate for both of your connections. These type of connections are asynchronous, so there's no guarantee that A will complete before B. Your code should handle whatever order the web server returns data in.
I suppose you could make the two methods synchronous (don't start B until A completes), but I don't think there's really any need to do that.
The good news is that the NSURLConnectionDelegate callbacks pass you the NSURLConnection object, so you can use that to determine whether you're getting a response to A or B. That information should tell you whether to put the data in the A or B web data object, and whether to update table view A or B, when the request completes. For example:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// determine which request/connection this is for
if (connection == connectiona) {
[webDataa appendData: data];
} else if (connection == connectionb) {
[webDatab appendData: data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// determine which request/connection this is for
if (connection == connectiona) {
NSLog(#"Succeeded! Received %d bytes of data",[webDataa length]);
// TODO(?): update the data source for UITableView (A) and call:
[tableViewA reloadData];
} else if (connection == connectionb) {
NSLog(#"Succeeded! Received %d bytes of data",[webDatab length]);
// TODO(?): update the data source for UITableView (B) and call:
[tableViewB reloadData];
}
// release the connection* and webData* objects if not using ARC,
// otherwise probably just set them to nil
}
This solution requires you keeping connectiona and connectionb as persistent ivars, not local variables in the code you posted. It looks like you're probably doing that, but since you don't show their declaration, I just wanted to be sure.
You should also implement the other delegate callbacks, of course, but the above two should give you a good example of the general solution.

iOS HTTP Request called twice

I have a function which makes GET request to the server. It works fine but for some reason it's called twice. I call function when a button is pressed.
This is function code:
-(void) GETasync: (NSString *) path{
receivedData = [[NSMutableData alloc] init];
NSURLRequest *request=[NSURLRequest requestWithURL:
[NSURL URLWithString: path]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
NSHTTPURLResponse * response;
NSError * error;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"data received");
}
So I see in output:
2012-03-07 16:36:41.509 KW2[24136:bf03] data received
2012-03-07 16:36:41.694 KW2[24136:bf03] data received
I also have a function for POST request and it's the same trouble with it.
I am assuming that you are printing out that log within your delegate method connection:didReceiveData:. That method can be called multiple times for one single connection - in fact it is commonly called at least twice.
From the documentation:
The delegate is periodically sent connection:didReceiveData: messages as the data is received. The delegate implementation is responsible for storing the newly received data.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
EDIT: I see that in your latest edits you have added that logging information (or maybe I did not see it right away - shame on me).
Are you possibly calling that method by a notification handler? If so, have you possibly double-registered for that notification -> hence your handler is invoked twice.
The problem was just in that I wired IBOutlets from File'Owner and First Responder to buttons in IB.
After removing wires from File's Owner, the method started to be called only one time.
Also make sure to check the IBAction connections for your button in Interface Builder. If you copy and paste buttons in IB, you can end up having 2 or more identical IBAction connections for a button, which would cause the method to be executed twice with just one button click.

ios get page source from web?

I am working on a app and try to display part of a web page.
My idea is first, get page source then parse it!
I already found a useful HTML parser for it but still struggling in how to get page source?
All I found is about UIWebview. Something like:
uiwebview = [[UIWebView loadRequest:[NSURLRequest requestWithURL:url]];
Use
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"] encoding:NSUTF8StringEncoding error:nil];
The disadvantage to using +stringWithContentsOfURL: in the main thread is that your UI will block while the request is active. This becomes especially problematic when the user is on a network with high latency, when the server is slow to respond, or if the request ends up timing out. In the last case, the user may see the UI block for a very long time.
The +stringWithContentsOfURL: method also lacks a way to provide you with error information in the event the server does not return a 200 status.
To perform the request asynchronously without blocking the UI, use NSURLConnection and grab the data in the delegate:
- ( void )connection: (NSURLConnection *)connection didReceiveData: (NSData *)data
{
// receivedData is an NSMutableData object
[ receivedData appendData: data ];
}
And then kick off parsing when the connection finishes:
- ( void )connectionDidFinishLoading: (NSURLConnection *)connection
{
[ self parseHTMLData: receivedData ];
}
The URL Loading System Programming Guide will get you started.

Resources