0

I was following these question and tutorials:

But I am still missing something. My delegate method

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

gets called all the time, takes a little bit time and I guess its because of internet speed, but the method

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

never gets called. What am I missing? Please help me.

My code:

-(void)loadDataBase{
    NSString *post = [NSString stringWithFormat:@"advnr=123"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://thewebsite.abc/interface.php"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    //return @"";
}

#pragma mark -
#pragma mark NSURLConnection delegates

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
    NSLog(@"Success"); // never gets executed
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"didFinished");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"FAILED");
}
Community
  • 1
  • 1
  • This delegate method is only call when the webservice return data, are you sure that you'r post request returns data other than a 200 http header ? – Boris Charpentier Oct 12 '14 at 18:57
  • yes, when I test it with my html test form I always get data that I need on the screen. –  Oct 12 '14 at 18:59

1 Answers1

0

I suspect that there is truly no data being returned (either because of some web service error or incorrectly prepared request).

You can use Charles to observe the request and the response to confirm this, if you'd like. BTW, Charles, or any tool like this, is a useful weapon to add to your arsenal, as it greatly simplifies diagnosing HTTP problems, isolating network related issues from client-side programming issues.

Regardless, if you get these sorts of errors, you will often get a non-200 statusCode returned by the server. See list of HTTP status codes.

You can verify this by also implementing didReceiveResponse and inspect the header of the response:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        int statusCode = [(NSHTTPURLResponse *)response statusCode];
        if (statusCode != 200) {
            NSLog(@"Status code = %ld; response = %@", (long)statusCode, response);

            // handle this error however you'd like
        }
    }

    // temporarily, you might even want to log the whole `response` regardless of the status code
    NSLog(@"%s: %@", __PRETTY_FUNCTION__, response);
}
Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I get `{ status code: 200, headers { Connection = close; "Content-Length" = 0; "Content-Type" = "text/html"; Date = "Sun, 12 Oct 2014 19:21:08 GMT"; Server = "Apache/2.2.22"; "X-Powered-By" = "PHP/5.3.26"; } }` but I can't understand why... If I use html test form I always get my data.. –  Oct 12 '14 at 19:23
  • That's encouraging. We want to see `200`. So the next question is whether the server truly returned nothing or not. This is where Charles becomes invaluable. – Rob Oct 12 '14 at 19:24
  • Content length is 0... that means I get no data,right? –  Oct 12 '14 at 19:26
  • Yep. Which makes me suspect a server problem or API problem. – Rob Oct 12 '14 at 19:27
  • Very strange... I always get data when im using html test form. Thanks for help. –  Oct 12 '14 at 19:32
  • 1
    Yeah, sometimes there are cookies involved. Or a field name has been misspelled. Or some hidden field that the server is looking for. In some cases, the web service is actually looking for the `User-Agent` header field (to confirm it's a web browser). Hard to say without seeing HTML. Did I mention that Charles is useful here (watch web browser request vs app request and compare)? lol. Good luck! – Rob Oct 12 '14 at 19:35