No data being fetched from webpage


#1

Hi

I’m trying the example in the callbacks chapter to fetch text from a webpage and print to the console but nothing is being fetched…
here is the output from console…

[Switching to process 516 thread 0x0]
2012-01-06 11:03:05.053 Callbacks[516:707] Got it all !
2012-01-06 11:03:05.057 Callbacks[516:707] string has 0 characters
2012-01-06 11:03:05.058 Callbacks[516:707] the whole string is

Can anyone spot where I’ve gone wrong or what the prob may be?

#import <Foundation/Foundation.h>
#import “Logger.h”

int main (int argc, const char * argv[])
{

@autoreleasepool {
    
    Logger *logger = [[Logger alloc]init];
    
    NSURL *url =[NSURL URLWithString:@"http://www.gutenberg.org/cache/epub/205/pg205.txt"];
    
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    
    __unused NSURLConnection *fetchConn = [[NSURLConnection alloc] initWithRequest:request delegate:logger startImmediately:YES];       
    
    /*
    __unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2
                                                       target:logger
                                                     selector:@selector(sayOuch:) 
                                                     userInfo:nil repeats:YES];
    */
    
    [[NSRunLoop currentRunLoop]run];        
}
return 0;

}

#import “Logger.h”

@implementation Logger

-(void)sayOuch:(NSTimer *)t
{
NSLog(@“ouch!”);
}

//called each time a chunk of data arrives
-(void)connection:(NSURLConnection *) didReciveData:(NSData *)data
{
NSLog(@“recieved %lu bytes”,[data length]);

//create a mutableData if it doesnt already exixt
if(!incomingData)
{
    incomingData = [[NSMutableData alloc]init];
}

[incomingData appendData:data];

}

//called when the last chunk has ben processed
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@“Got it all !”);
NSString *string = [[NSString alloc] initWithData:incomingData encoding:NSUTF8StringEncoding];

incomingData = nil;
NSLog(@"string has %lu characters",[string length]);

//uncomment the next line to see the whole fetched file
NSLog(@"the whole string is %@",string);

}

//called if the fetch fails
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@“connection failed: %@”,[error localizedDescription]);
incomingData =nil;
}

@end

Thanks for any help

Lenny


#2

Do you have your incomingData ivar declared in Logger.h?

[code]#import <Foundation/Foundation.h>

@interface Logger : NSObject
{
// nsmutabledata to hold the data from a url
NSMutableData *incomingData;
}

-(void)sayOuch:(NSTimer *)t;
@end[/code]


#3

Hi

Yea its set up ok …

#import <Foundation/Foundation.h>

@interface Logger : NSObject
{
NSMutableData *incomingData;
}
-(void)sayOuch:(NSTimer *)t;

@end

I’m sure I just have a typo somewhere but can’t spot it …

Thanks


#4

The problem is a misspelling:

Receive is misspelled.


#5

Thanks Aaron

Nice one…working perfect now…

Lenny

ps … Kudos on the book… first rate !


#6

Hi,
I have the same problem but didn’t find the typo.
The french translator had the stupid idea to translate the program source (!!!) but I hope you will make the relation with the original in English.
enregistreur.h :

#import <Foundation/Foundation.h>

@interface Enregistreur : NSObject <NSURLConnectionDelegate,NSURLConnectionDataDelegate>
{
    NSMutableData *donneesEntrantes ;
}
- (void) direAie:(NSTimer *)t ;
@end

enregistreur.m :

#import "Enregistreur.h"

@implementation Enregistreur
-(void) direAie:(NSTimer *)t
{
    NSLog(@"Aie") ;
}

// arrivée au moins d'une trame de données
- (void) connection:(NSURLConnection*)connexion
    didReceivedData:(NSData *)donnees
{
    NSLog(@"Nous avons %lu octets",[donnees length]) ;
    if ( ! donneesEntrantes){
        donneesEntrantes = [[ NSMutableData alloc]init] ;
    }
    [donneesEntrantes appendData:donnees] ;
}

// arrivee de la dernière trame
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Nous avons tout reçu") ;
    NSString *chaine = [[ NSString alloc] initWithData:donneesEntrantes encoding:NSUTF8StringEncoding] ;
    donneesEntrantes =nil ;
    NSLog(@"la chaine contient %lu caractères",[chaine length]) ;
    NSLog(@"->%@<-",chaine) ;
}

// si echec de la recup
- (void) connection:(NSURLConnection*)connexion
   didFailWithError:(NSError *)erreur
{
    NSLog(@"echec de la connexion: %@",[erreur localizedDescription]) ;
    donneesEntrantes = nil ;
}
@end
and main.m
#import <Foundation/Foundation.h>
#import "Enregistreur.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Enregistreur * enregistreur = [[Enregistreur alloc]init] ;
        
        NSURL *url = [NSURL URLWithString:@"http://www.gutenberg.org/cache/epub/205/pg205.txt"] ;
        if ( !url) NSLog(@"PB url") ;
        NSURLRequest *requete = [NSURLRequest requestWithURL:url] ;
        if ( !requete) NSLog(@"PB requete") ;
        __unused NSURLConnection *connexion = [[NSURLConnection alloc]initWithRequest:requete delegate:enregistreur startImmediately:YES] ;
        if ( !connexion) NSLog(@"PB connexion") ;
        

        __unused NSTimer *minuterie = [NSTimer scheduledTimerWithTimeInterval:2.0 target:enregistreur selector:@selector(direAie:) userInfo:nil repeats:YES] ;
        [[NSRunLoop currentRunLoop] run] ;
    }
    return 0;
}

and the output I get :

2013-07-28 00:12:40.804 Rappels[25980:303] Nous avons tout reçu
2013-07-28 00:12:40.805 Rappels[25980:303] la chaine contient 0 caractères
2013-07-28 00:12:40.806 Rappels[25980:303] -><-
2013-07-28 00:12:40.881 Rappels[25980:303] Aie
2013-07-28 00:12:42.881 Rappels[25980:303] Aie
2013-07-28 00:12:44.881 Rappels[25980:303] Aie

Thanks for any help.


#7

I got the typo !
It was a “d” in the didReceivedData …
Anyway I’m happy to read this book.
But it will a good idea to not translate the source and have a repository of them.

have a nice day.