0

我想在单个视图控制器类中创建两个连接。我为此目的使用 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");
    }

}
4

1 回答 1

0

您在第一个链接中提供的 URL 似乎是 borken ... 看看这个“ http://www.google.com/ig/api?weather=New Delhi”.. 新德里之间有空格。试试这个http://www.google.com/ig/api?weather=New+Delhi

于 2011-12-13T10:59:47.993 回答