Асинхронное соединение не вызывается - PullRequest
0 голосов
/ 13 декабря 2011

Я хочу создать два соединения в одном классе контроллера представления.Я использую NSOperationQueue для этой цели.Два соединения создаются в двух функциях и помещаются внутрь очереди. Проблема в том, что делегаты не вызываются.Пожалуйста, помогите мне.Заранее спасибо

- (void)viewDidLoad {
    [super viewDidLoad];
    NSOperationQueue *queue=[[NSOperationQueue alloc] init];
    NSInvocationOperation *invOperation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createConnection1) object:nil];
    NSInvocationOperation *invOperation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createConnection2) object:nil];
    NSArray *ops=[[NSArray alloc] initWithObjects:invOperation1,invOperation2,nil];
    [queue addOperations:ops waitUntilFinished:YES];

}


-(void) createConnection1{

    //create connection
    NSLog(@"Create Connection 1");
    url1 =[[NSMutableString alloc] initWithFormat:@"http://www.google.com/ig/api?weather=New Delhi"]; 

    NSURLRequest *theRequest1=[NSURLRequest requestWithURL:[NSURL URLWithString:url1]
                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                           timeoutInterval:60.0];
    theConnection1=[[NSURLConnection alloc] initWithRequest:theRequest1 delegate:self];
    if (theConnection1) {
        connectionCreated1=YES;
        receivedData1 = [[NSMutableData data] retain];  
        NSLog(@"received data 1 %@",receivedData1);
        //[theConnection1 setDelegate:self];
    } 
}

-(void) createConnection2{
    //create connection
    NSLog(@"Create Connection 2");
    url2 =[[NSMutableString alloc] initWithFormat:@"http://www.google.com/ig/api?weather=Chennai"]; 

    NSURLRequest *theRequest2=[NSURLRequest requestWithURL:[NSURL URLWithString:url2]
                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                           timeoutInterval:60.0];
    theConnection2=[[NSURLConnection alloc] initWithRequest:theRequest2 delegate:self];
    if (theConnection2) {
        connectionCreated2=YES;
        receivedData2 = [[NSMutableData data] retain];
        //[theConnection2 setDelegate:self];
        NSLog(@"received data 2 %@",receivedData2);
    } 

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    if (connectionCreated1==YES) {
        [receivedData1 setLength:0];
    }
    else if (connectionCreated2==YES) {
        [receivedData2 setLength:0];
    }
    else {
        NSLog(@"did not receive response");
    }


}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    if (connectionCreated1==YES) {
    [theConnection1 release];
    xmlParser1 = [[NSXMLParser alloc] initWithData:receivedData1];
        [xmlParser1 setDelegate:self];
    [xmlParser1 parse];
    }
    else if(connectionCreated2==YES){
        [theConnection2 release];
        xmlParser2 = [[NSXMLParser alloc] initWithData:receivedData2];
        [xmlParser2 setDelegate:self];
        [xmlParser2 parse];

    }



}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"connection failed" message:@"" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (connectionCreated1==YES) {
        [receivedData1 appendData:data];
    }
    else if(connectionCreated2==YES) {
        [receivedData2 appendData:data];
    }
    else {
        NSLog(@"data not received");
    }

}

1 Ответ

0 голосов
/ 13 декабря 2011

URL, который вы указали в первой ссылке, кажется неправильным ... посмотрите на это "http://www.google.com/ig/api?weather=New Delhi" .. между Нью-Дели есть пробел. попробуйте это вместо http://www.google.com/ig/api?weather=New+Delhi

...