Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSURLConnection不返回数据_Iphone_Cocoa_Nsurlconnection - Fatal编程技术网

Iphone NSURLConnection不返回数据

Iphone NSURLConnection不返回数据,iphone,cocoa,nsurlconnection,Iphone,Cocoa,Nsurlconnection,我这里有一个相当不寻常的问题。下面的代码运行良好,数据发送到源代码,但NSURLConnection触发器均未返回任何内容。正在记录的唯一项目是发送请求的函数中的项目。有什么想法吗 // code starts above here NSData *myRequestData = [ NSData dataWithBytes: [ requestString UTF8String ] length: [ requestString length ] ]; NSURLRe

我这里有一个相当不寻常的问题。下面的代码运行良好,数据发送到源代码,但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]);
}
这就是问题所在:

NSData *response = [NSURLConnection sendSynchronousRequest:request 
                                         returningResponse:&resp 
                                                     error:&err];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request 
                                                               delegate:self 
                                                       startImmediately:YES];
首先启动一个同步请求,然后再次启动该请求,但不是异步的。毫无意义


尝试删除第一行。

关于-连接:willSendRequest:redirectResponse:?另外,您应该始终实现-connection:didFailWithError:。抱歉,这里有一个connection:didFailWithError函数,但它在我的代码中位于较低的位置