2010-01-23 86 views
1

我在这里有一个相当不寻常的问题。下面的代码运行良好,数据发送到源,但没有NSURLConnection触发器返回任何东西。唯一记录的项目是发送请求的函数。任何想法?NSURLConnection不返回数据

// code starts above here 

    NSData *myRequestData = [ NSData dataWithBytes: [ requestString UTF8String ] length: [ requestString length ] ]; 
    NSURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlPrefix]]; 
    [request setHTTPMethod: @"POST"]; 
    [request setHTTPBody: myRequestData]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
    NSURLResponse *resp = nil; 
    NSError *err = nil; 
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse: &resp error: &err]; 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

    if (!theConnection) 
    { 
     NSLog(@"Failed to submit request"); 
    } 
    if(theConnection) 
    { 
     NSMutableData* receivedData=[[NSMutableData data] retain]; 
     NSLog(@"Created connection."); 

     NSLog(@"--------- Request submitted ---------"); 
     NSLog(@"receivedData: %@", receivedData); 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"Received response: %@", response); 
    NSLog(@"Received response, connection retain count is %d",[connection retainCount]); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"Connection received data, retain count: %d", [connection retainCount]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    NSLog(@"finished connection retain count: %d", [connection retainCount]); 
} 
+1

-connection:willSendRequest:redirectResponse:?另外,你应该总是实现-connection:didFailWithError :. – Costique 2010-01-23 10:49:46

+0

对不起,有一个连接:didFailWithError函数,但它的代码更深入 – Matt 2010-01-23 23:20:04

回答

2

这就是问题所在:

NSData *response = [NSURLConnection sendSynchronousRequest:request 
             returningResponse:&resp 
                error:&err]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request 
                   delegate:self 
                 startImmediately:YES]; 

首先启动同步请求,然后你开始请求再次但比异步的。没有意义。

尝试删除第一行。

+0

啊,哎呀,谢谢你! – Matt 2010-01-23 23:27:04