NSURLConnection代表问题

时间:2011-06-04 08:12:10

标签: iphone objective-c nsurlconnection

NSURLConnection委托方法未调用(didReceiveResponse,connectionDidFinishLoading等)。当我使用相同的类委托方法正确调用时

category *cat = [[category alloc]init];  
    [cat getcategory:@"1088"]; 

在类别页面中

(void) getcategory: (NSString *) catid {
[...]

 NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlrequest delegate:self];  

        NSHTTPURLResponse *response;  
       [NSURLConnection sendSynchronousRequest: urlrequest returningResponse: &response error: nil];   

        if( theConnection )  
        {  
            webData = [[NSMutableData data] retain];  
        }  
        else  
        {  
            NSLog(@"theConnection is NULL");  
        }  

    }  

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {   
        [webData setLength: 0];  
    }  
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
    {  
        [webData appendData:data];  
    }  
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {  
        NSLog(@"ERROR with theConenction");  
        [connection release];  
        [webData release];  
    }  
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {  
        NSLog(@"DONE. Received Bytes: %d", [webData length]);  
        NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:  [webData length] encoding:NSUTF8StringEncoding];  
    }

1 个答案:

答案 0 :(得分:1)

您在2个不同的连接中使用相同的请求:theConnection(这是异步的,并且设置了委托 - 连接在启动时开始)和[NSURLConnection sendSynchronousRequest: urlrequest returningResponse: &response error: nil];。尝试删除第二个