views:

41

answers:

1

Hello,

I have a NSURLConnection that receives data output from a url pointing to a php script on my server.

Most of the time everything works fine and the data is retrieved in its complete form.

However, sometimes I receive NULL or broken (ie the bottom half) of data at:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

When this happens, if I reload the connection it will always return the same null or broken block of data for the request.

EDIT*: I've realized that when I receive what I thought was nil data, I actually received data but the NSString created from this data is nil. I still don't understand why though. My php encoding output is always UTF-8 so I don't think it is an issue of encoding and besides it works most of the time with this.

I have checked the php script with that same request to verify that it is not a problem on the server side or with the php script and confirmed that it is NOT.

My code is Below:

-(void)setUpConnectionAndMakeRequest {

            NSString *URLpath = @"http://www.mydomain.com/myphp.php"; 
    NSURL *myURL = [[NSURL alloc] initWithString:URLpath];

    NSMutableURLRequest *myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    [myURL release];
    [myURLRequest setHTTPMethod:@"POST"];


    //I added this because I thought it may be a problem relating to cache but it isn't
    NSURLCache *cache = [NSURLCache sharedURLCache]; 
    [cache removeAllCachedResponses];


    NSString *httpBodystr = [NSString stringWithString:@"command=runscript"];


    [myURLRequest setHTTPBody:[httpBodystr dataUsingEncoding:NSUTF8StringEncoding]];


    [mailData setData:nil]; //mailData is a NSMutableData object which accumulates the data retrieved by the request

    [NSURLConnection connectionWithRequest:myURLRequest delegate:self];

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

NSString *untrimmedDataSTR = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  //Created so I can see the data (always text) in NSLog
NSLog(@"Live Data: %@", untrimmedDataSTR);   //This is where I see that the data is broken or null when it shouldn't be


[mailData appendData:data];  //Append accumulated data to NSMutableData object used later in my app.

[untrimmedDataSTR release]; 

}

I've been scratching my head on this for days so any help would be much appreciated. Thanks.

+2  A: 

According to the NSString reference, -initWithData:encoding: returns nil if "the initialization fails for some reason (for example if data does not represent valid data for encoding)."

That almost certainly means that the response from the server is not, in fact, UTF-8 encoded data.

The way to check would be to NSLog the data before trying to convert to an NSString:

NSLog(@"Raw Data: %@", data);

(The -description method on NSData will return a hexadecimal representation of the contents; that's what will get logged).

David Gelhar