1

ここでかなり珍しい問題があります。以下のコードは正常に実行されており、データは正常にソースに送信されていますが、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]);
}
4

1 に答える 1

2

これが問題です:

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

最初に同期リクエストを開始し、次にリクエストを再度開始しますが、非同期ではありません。意味がありません。

最初の行を削除してみてください。

于 2010-01-23T15:15:14.057 に答える