Note that there are some explanatory texts on larger screens.

plurals
  1. PONSURLConnection Data broken in asynchronous connection
    primarykey
    data
    text
    <p>I have a NSURLConnection that receives data output from a url pointing to a php script on my server.</p> <p>Most of the time everything works fine and the data is retrieved in its complete form.</p> <p>However, sometimes I receive NULL or broken (i.e. the bottom half) of data at:</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data </code></pre> <p>When this happens, if I reload the connection it will always return the same null or broken block of data for the request.</p> <p>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.</p> <p>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.</p> <p>My code is Below:</p> <pre><code>-(void)setUpConnectionAndMakeRequest { NSString *URLpath = @"http://www.example.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]; } </code></pre> <p>Any help would be much appreciated. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload