Objective-C JSON Parse don't work in other classes - iphone

I have a strange problem with the NSURLConnectionDataDelegate.
I have created a class for parsing of json.
Here the code:
- (void)createJSONDictionaryFromURL:(NSURL *)url
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
jsonData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[jsonData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Fail with error");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSLog(#"%#", self.jsonDictionary);
}
In the Method connectionDidFinishLoading, is everything correct, the console shows the correct data.
But I want to use this in the viewDidLoad Method from my ViewController.
JSONParser *parser = [[JSONParser alloc] init];
NSDictionary *dict = [[NSDictionary alloc] init];
[parser createJSONDictionaryFromURL:url];
dict = parser.jsonDictionary;
NSLog(#"%#", dict);
The console shows (null), can anybody help me?
Sorry for my bad english

Your JSONParser is asynchronous because of the network connection (it is asynchronous). So, when you currently call parser.jsonDictionary you see that the data isn't ready yet.
You don't want to try blocking until the data is available, rather you should setup a callback to get the data to your view controller when it's ready (so you can save it and update the UI).
That could be done by adding a delegate property to your JSONParser and having the view controller assign itself and implement some callback method. Or, using less code, the JSONParser can offer a block property that the view controller sets and which is called in connectionDidFinishLoading: (make sure the #property uses (copy, nonatomic)`).

Related

How to use asynchronous web service call for auto complete TextField in iphone?

I want auto filled UITextFiled in my app. In it when user type some letter it will call web service and show response in UIpPickerView like to search city. When we type any letter it shows some city names. Can anyone knows how to do it? Please help me.
To get data from server asynchronously you can use the NSURLConnection and NSURLConnectionDelegate methods
In interface file:
#interface ViewController : UIViewController<NSURLConnectionDelegate, UITextFieldDelegate> {
NSMutableData *mutableData;
}
-(void)getDataUsingText:(NSString *)text;
#end
In implementation file:
#implementation ViewController
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value =[textField.text stringByReplacingCharactersInRange:range withString:string];
[self getDataUsingText:value];
return YES;
}
-(void)getDataUsingText:(NSString *)text;
{
NSString *urlString = [NSString stringWithFormat:#"http://...."];
NSURL *url =[NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
mutableData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mutableData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *dataString = [[NSString alloc] initWithData:mutableData encoding:NSUTF8StringEncoding];
NSLog(#"your data from server: %#", dataString);
// Here you got the data from server asynchronously.
// Here you can parse the string and reload the picker view using [picker reloadAllComponents];
}
#end
You must set the delegate to the text field and you have to implement the picker using the data from the NSURLConnectionDelegate method. And this is a tutorial for loading picker view.

NSUrlConnectionDelegate not calling methods to load data

I have looked at NSURLConnectionDelegate connection:didReceiveData not working already, but there didn't seem to be any good result from that, so I am curious why I am not able to get any data.
I put in breakpoints in didReceiveResponse and didReceiveData.
It does print out "connection succeeded", so I know that the connection is started.
I am using ARC for memory management.
- (void)load {
request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
[conn start];
NSLog(#"connection succeeded, %s", [myURL description]);
responseData = [NSMutableData data];
} else {
NSLog(#"connection failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
UPDATE:
To see how I test this look at Asynchronous unit test not being called by SenTestCase.
I did implement the two methods mentioned by jonkroll, in his answer, I just didn't show them, but, they also aren't being called.
I had added [conn start] only because it wasn't working, and I was hoping that may solve it, but no such luck.
When you declare your connection like this:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
You are creating a local pointer. When your method completes, since it was the last strong reference to the NSURLConnection, ARC releases it. You need to use a strong ivar (and/or) property to hold a strong reference to the NSURLConnection you create.
Edit
Here is basic sample of code that I tested in a sample project. Give it a run. Verbose logging helps.
#implementation <#Your class here#> {
// With ARC ivars are strong by default
NSMutableData *_downloadedData;
NSURLConnection *_connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
if (realResponse.statusCode == 200){
// Really any 2** but for example
_downloadedData = [[NSMutableData alloc] init];
NSLog(#"Good response");
} else {
NSLog(#"Bad response = %i",realResponse.statusCode);
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (connection == _connection){
[_downloadedData appendData:data];
NSLog(#"Getting data...");
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection == _connection){
_connection = nil;
NSLog(#"We're done, inform the UI or the delegates");
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
_connection = nil;
NSLog(#"Oh no! Error:%#",error.localizedDescription);
}
- (void)load {
NSURL *url = [NSURL URLWithString:#"http://www.google.com/"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
// Assign strong pointer to new connection
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(#"Connection was initialized? = %#",(!!_connection)?#"YES":#"NO");
}
#end
The NSURLConnection method initWithRequest starts an asynchronous request for data from a url. Because the request is done asynchronously you can't expect to work with the response in the same method in which the request is invoked. Instead you need to do so in the NSURLConnection's delegate callback methods. You have already implemented didReceiveResponse: and didReceiveData:, but there are a couple others that will be useful to you.
If you want to look at the contents of the response you should do so in connectionDidFinishLoading:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// response is complete, do something with the data
NSLog(#"%#", responseData);
}
The fact that your code prints out "connection succeeded" doesn't really mean that the request was successful, only that the NSURLConnection object was created successfully. To test whether there was a problem with the connection you can implement the delegate method connection:didFailWithError:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Connection failed! Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
Also there is no need to call [conn start]. The request will be started automatically when you call initWithRequest:
I suggest reading Apple's documentation on Using NSURLConnection for more details.

why nsurldelegates method are not called when json parsing in iphone

I am developing one iphone application from google places API. For that I am doing JSON parsing for getting data.
For JSON parsing I have made one class and in which I'm writting these methods
in JsonParse.m file following methods are written:
- (void)initWithURLString:(NSString *)aURLString parameter:(UIViewController *)viewController
{
NSURL *url = [NSURL URLWithString:aURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
connection=[[NSURLConnection alloc] initWithRequest:request delegate:viewController];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
label.text=[NSString stringWithFormat:#"error in the connection %#",[error description]];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responsestring=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"response string is %#",responsestring);
NSError *error;
SBJSON *json=[[SBJSON new]autorelease];
NSMutableDictionary *response=[NSMutableDictionary dictionary];
response=[json objectWithString:responsestring error:&error];
NSLog(#"values in the dictionary is %#",[response valueForKey:#"results"]);
}
And I am calling the method from the viewcontroller class as below:
JsonParse *obj=[[JsonParse alloc] init];
[obj initWithURLString:urlstr parameter:self];
But when I am debugging only initwithurl method is called, other connection delegate methods are not called.
Previously I have written the methods in the same class means in the viewcontroller class at that time every method was called and I was able to prase data.
I have written this method in seperate class because in the same viewcontroller class I want to parse data multiple times (more that 1 time with different urls).
Does anyone know why these methods are not called or how can I parse multiple times in the same class? Any tutorial or sample code for that?
In :
// the delegate must be self
connection=[[NSURLConnection alloc] initWithRequest:request
delegate:viewController];
The delegate should be self in order for these methods to get called, because these methods are implemented in JsonParse.

Multiple NSURLCoonections in same View using iphone sdk

Can any one of you post the code snippet, example tutorial on handling the multiple
NSURLConnections from the same viewController using cocoa Touch framework....
Thanks for all your future help.....
I handled multiple NSUrlConnections using a NSMutableDictionary which keeps track of which instance of NSMutableData a particular NSURLConnection should save its result to.
At the beginning of my class I define:
NSMutableDictionary *dataDictionary;
Then in my loadData method, I have:
// Create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:currentCam]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
NSMutableData *receivedData = receivedData = [[NSMutableData alloc] init];
//keep track of this connection by adding it to subViewDictionary and dataDictionary with appropriate objects.
[dataDictionary setObject:receivedData forKey:[theConnection description]];
}
else {
NSLog(#"ERROR DOWNLOADING WITH NSURLCONNECTION");
}
[theConnection release];
I use [theConnection description] as my key and an instance of MSMutableData as the object in my dictionary, so later I can look up which instance goes with a particular connection. If you fail to do this you can have issues with data corruption (multiple connections can all save their data to the same variable).
Then, I define the following NSURlConnection delegate methods:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data
{
//look up in dictionary to find out which recievedData instance to use.
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
// Append the new data to receivedData.
[theReceivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//Lookup in the dictionary to find the correct instance of recievedData to put image into.
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
[theReceivedData setLength:0];
[activityIndicator stopAnimating];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// inform the user that there was an error
NSLog(#"Connection failed! Error - localizedDescription:%# NSURLErrorFailingURLStringErrorKey:%#",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
[theReceivedData release];
//remove keys for this connection since it did not load.
[dataDictionary removeObjectForKey:[connection description]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//look up correct instance of recievedData in teh dictionary for this connection
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
NSLog(#"Succeeded! Received %d bytes of data",[theReceivedData length]);
//---do stuff with data here//
[theReceivedData release];
}
There is a good tutorial on NSURlConnection here for a single connection. My code is based on that with the addition of the NSMutableDictionary to keep track of each NSUrlConnection and each NSMutableData instance.
I hope that makes sense and is helpful!
See this link for the answer. It wraps nsurlconnection with asihttp, which makes your life much easier.
Does ASIHTTP support multi threads?

Get URL File Size before parsing

Me again! I am using NSURL to get a file then parse it. I have been looking for a couple of hours now on a progressbar i am trying to implement in my app. I know I first need to get the file size, then keep updating how much data i've downloaded as I continue to pull. I have seen the example using "ASIHTTPRequest" but is there a way to do it with what I already have?
Here is where I start the download.
-(void)parseNewData {
//start network activity spinner and release controller when done
parserDone = NO;
[root downloadIcon];
//create pool to avoid memory leak
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// get the XML path and start parsing
NSURL *pathURL = [NSURL URLWithString:#"http://www.mysite.com/myfile.xml"];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:pathURL];
[parser setDelegate:self];
[parser parse];
//drain pool
[pool drain];
[pool release];
}
Can someone point me in the right direction on how to get file size, then how to update how much I've downloaded. Thanks in advance!
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
CGFloat size = [[NSString stringWithFormat:#"%lli",[response expectedContentLength]] floatValue];
NSLog(#"Size : %f",size);
}
Above code will give you total size
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
totalDownloadedData += [data length]; // global integer
NSLog(#"Status : %d/%f",totalDownloadedData,size);
}
Above code will show you current status of downloading
You need to use NSURLConnection if you want to get the file size and the progress. You get delegate methods which you can use to monitor progress. The didSendBodyData: delegate method tells you how much data there is in bytes. The connectionDidFinishLoading is where you uget receivedData to use in your NSXMLParser code.
NSURLRequest *theRequest = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
// release the connection, and the data object
// inform the user
NSLog(#"Connection failed! Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{