4
-(void)loadRequest:(NSString *)jsonString{
receivedData = [[NSMutableData alloc]init];
urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:kURL]];
urlRequest=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   requestInformation =[[NSMutableURLRequest alloc]initWithURL:urlRequest cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
[requestInformation setValue:khttpValue forHTTPHeaderField:kContentType];
[requestInformation setValue:@"value1" forHTTPHeaderField:@"key1"];

[requestInformation setHTTPMethod:kPost];
jsonData= [[NSString stringWithFormat:@"json=%@",jsonString] dataUsingEncoding:NSUTF8StringEncoding];
[requestInformation setHTTPBody:jsonData];

connection = [[NSURLConnection alloc] initWithRequest:requestInformation delegate:self];
[connection start];
if(connection){
    NSLog(@"Connection succesfull");
}
else{
    NSLog(@"There is a error in connection");
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(onFailedToUpload) userInfo:nil repeats:NO];

}
}

how to check if the else part is executed. I give incorrect URL , didfailwitherror method is called but the else part is not executed.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Ben10
  • 3,221
  • 2
  • 34
  • 61
  • 1
    If you want to handle bad URLs, then do the checking on the URL, not the connection. See: http://stackoverflow.com/questions/1471201/how-to-validate-an-url-on-the-iphone ... http://stackoverflow.com/questions/192944/whats-the-best-way-to-validate-a-user-entered-url-in-a-cocoa-application – Mazyod Sep 10 '12 at 06:46

1 Answers1

3

As far as I know, the connection object is always created. Even of your url is wrong. Any errors whatsoever comes to the delegate didFailWithError method. You probably need to examine the error & proceed appropriately. Ex. In case it is a timeout you might want to retry in didFailWithError delegate. For other error types handle differently.

If you want to handle broken or bad urls before passing it to NSURLConnection then you need to do that yourself.

Here are the delegates which are of use to you when using NSURLConnection -

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response 
{
    NSLog("@Resp received");
    return;
}

- (void)connection:(NSURLConnection *)connection
    didReceiveData:(NSData *)data 
{
    NSLog("@Data received");
    return
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error 
{
    NSLog("@ERROR: Achtung !: %@",[error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH , 0),, ^{
        NSLog(@"FinishedLoading: In bg thread, do something with data here");

        dispatch_async( dispatch_get_main_queue(), ^{
            NSLog(@"FinishedLoading: In Main thread, access the UI here");
        });
    });
}
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264