Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.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
Ios 两次之后未调用NSURLConnection委托方法_Ios_Iphone_Ios7_Nsurlconnection_Nsurlconnectiondelegate - Fatal编程技术网

Ios 两次之后未调用NSURLConnection委托方法

Ios 两次之后未调用NSURLConnection委托方法,ios,iphone,ios7,nsurlconnection,nsurlconnectiondelegate,Ios,Iphone,Ios7,Nsurlconnection,Nsurlconnectiondelegate,我正在使用NSURLConnection向服务器发送请求,它工作了2次,因为第三次它不工作,代理没有被调用。我在调试过程中发现,connectionShouldUseCredentialStorage方法在初始时间被调用,第三次未被调用,其余方法也未被调用 这是我的密码: NSString *requestString = [NSString stringWithFormat:@"%@", [serviceParameters JSONFragment], nil]; NSDa

我正在使用
NSURLConnection
向服务器发送请求,它工作了2次,因为第三次它不工作,
代理
没有被调用。我在调试过程中发现,
connectionShouldUseCredentialStorage
方法在初始时间被调用,第三次未被调用,其余方法也未被调用

这是我的密码:

    NSString *requestString = [NSString stringWithFormat:@"%@", [serviceParameters JSONFragment], nil];

    NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: strUrl]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
    [request setHTTPBody:requestData];
    [NSURLConnection connectionWithRequest:urlRequest delegate:self];


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.data = [NSMutableData data];
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)aData
{
    [self.data appendData:aData];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    SBJSON *jsonParser = [[SBJSON new] autorelease];
    NSString *jsonString = [[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding] autorelease];
    NSError *outError = nil;

    id result = [jsonParser objectWithString:jsonString error:&outError];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}


- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    if( [[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust] )
    {
        return YES;
    }
    return NO;
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
    return YES;
}
in.h

还添加

英寸


调用
[自启动连接]无论您想在哪里,都意味着在ViewDiLoad或UI按钮上单击您忘记启动连接。您没有保存对NSURLConnection的引用以启动连接。所以,替换最后一行

[NSURLConnection connectionWithRequest:urlRequest delegate:self];
用这个

NSURLConnection *cn =[NSURLConnection connectionWithRequest:urlRequest delegate:self];
[cn start];

希望有帮助。

您是否尝试过在NSURL委托方法中设置断点?您还可以尝试增加超时。如果([NSURLConnection connectionWithRequest:urlRequest delegate:self]!=nil){NSLog(@“connectgion started”);}您是从主线程运行它,还是从连接到运行循环的线程(如果不是主线程)运行它?根据文档:“在调用此方法的同一线程上调用委托方法。要使连接正常工作,调用线程的运行循环必须在默认运行循环模式下运行。”@user2071152我尝试使用断点,并尝试使用超时300值,但是没有帮助解决问题。@satheeshwaran:我尝试了你的代码,它显示连接已启动。NSURLConnection不是零。
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
NSURLConnection *cn =[NSURLConnection connectionWithRequest:urlRequest delegate:self];
[cn start];