NSURL delegate didReceiveData method not called

2019-06-14 12:26发布

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

2条回答
叼着烟拽天下
2楼-- · 2019-06-14 13:07

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.

NSData *returnedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://yourURLHere"]];

if (returnedData) {
    NSLog(@"returnedData:%@", returnedData);

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:returnedData options:kNilOptions error:Nil];
    NSLog(@"jsonDict:%@", jsonDict);
}
else
    NSLog(@"Got Nothing");

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

#import <UIKit/UIKit.h>

@interface AsyncImage : UIImageView 
{    
    NSMutableData *activeDownload;
    NSURLConnection *imageConnection;
}
@property (nonatomic, retain) NSMutableData *activeDownload;
@property (nonatomic, retain) NSURLConnection *imageConnection;
- (void)loadImageFromURL:(NSURL*)url;
@end



#import "AsyncImage.h"

@implementation AsyncImage
@synthesize activeDownload;
@synthesize imageConnection;

- (void)dealloc
{
    [super dealloc];
    NSLog(@"dealloc AsyncImage");

    [activeDownload release];
    [imageConnection cancel];
    [imageConnection release];
}

- (void)cancelDownload
{
    [self.imageConnection cancel];
    self.imageConnection = nil;
    self.activeDownload = nil;
}


#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.activeDownload appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // Clear the activeDownload property to allow later attempts
    self.activeDownload = nil;

    // Release the connection now that it's finished
    self.imageConnection = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.image = [UIImage imageWithData:self.activeDownload];

    self.activeDownload = nil;

    // Release the connection now that it's finished
    self.imageConnection = nil;
}

- (void)loadImageFromURL:(NSURL*)url {
    if (self.imageConnection!=nil) { [imageConnection release]; } //in case we are downloading a 2nd image

    self.activeDownload = [NSMutableData data];
    // alloc+init and start an NSURLConnection; release on completion/failure
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                             [NSURLRequest requestWithURL:url] delegate:self];
    self.imageConnection = conn;
    [conn release];
}

@end
查看更多
祖国的老花朵
3楼-- · 2019-06-14 13:20

Set NSURLConnectionDelegate in the .h file.

查看更多
登录 后发表回答