开发者

NSURLConnection Not Returning Data

开发者 https://www.devze.com 2022-12-17 17:09 出处:网络
I have a rather unusual issue here. The code below is running fine and the data is sending to the source fine, but none of the NSURLConnection triggers are returning anything. The only items being log

I have a rather unusual issue here. The code below is running fine and the data is sending to the source fine, but none of the NSURLConnection triggers are returning anything. The only items being logged are in the function that the request is sent in. Any ideas?

    // 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-urlen开发者_高级运维coded" 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]);
}


This is the problem:

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

First you start a synchronous request and then you start the request again but than asynchronous. Makes no sense.

Try removing the first line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号