I know this has been discussed in a lot of other posts but I need to post it because those threads have not helped me unfortunately. I am trying to connect to a rails server which returns some data in JSON format in response to a GET request.I have implemented the four methods for NSConnectionDataDelegate as listed below
#pragma mark -
#pragma mark NSURLConnectionDataDelegate
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
[resultData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"didReceiveData");
[resultData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
NSString *errDesc = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
NSLog(@"%@",errDesc);
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"didReceiveLoading");
}
In the connectionDidFinishDownloading method (not listed) I carry out the JSON parsing but that fails since resultData is empty since didReceiveData method is never called.
The didReceiveResponse method is called because I see the message being logged. The server is running on localhost so I can confirm the request is received and using Charles I know the response is correctly received in JSON format. So there seems to be no problem on the server side. But the didReceiveData method is never called. Neither is didFailWithError method.
Can someone please help me with this? I cannot figure out what I am doing wrong. This is the first time I am working with NSURL so it would be much appreciated. I am working on iOS 5 with ARC.
Satyam
Edit:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?%@", kDevelopmentMode ? kLocalHost : kHost, endpoint, parameters]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
My guess would be that your app is not getting data back. Try this with your url it will let you know the raw data returned, and the jsonserialization if data is returned.
if your log displays the raw data and the json data then The issue is with your NSURLRequest. But if the log displays "Got Nothing" then there is a problem with what your trying to get.
EDIT
Set
NSURLConnectionDelegate
in the .h file.