ios - 两次后未调用 NSURLConnection 委托(delegate)方法

标签 ios iphone ios7 nsurlconnection nsurlconnectiondelegate

我正在使用 NSURLConnection 向服务器发送请求,它工作了 2 次,因为第三次它不工作,delegates 没有被调用。我在调试过程中发现 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;
}

最佳答案

在 .h 中

NSMutableData * data;
NSURLConnection *connection;

同时添加添加

.m

-(void) startConnection
{
    NSString *requestString = [NSString stringWithFormat:@"http://md5.jsontest.com/?text=Pushkraj"];

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

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestString]
                                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                     timeoutInterval:60.0];


    data = [NSMutableData dataWithCapacity:0];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
    [request setHTTPBody:requestData];
    connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
}

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


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)aData
{
    [data appendData:aData];
    NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSLog(@"dataDict : %@",dataDict);
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Error : %@",error);
}

调用 [self startConnection]; 无论你想在 viewDidLoad 或 UIButton Click 上调用

关于ios - 两次后未调用 NSURLConnection 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23670991/

相关文章:

ios - 如何修复错误: Generic parameter 'T' could not be inferred

ios - nw_connection_get_connected_socket_block_invoke 19 连接没有连接处理程序

iphone - 是否可以使 TableView 索引在搜索栏之后开始并使搜索栏固定在顶部?

iphone - AFJSONRequestOperation 数组填充但不能成功 block 之外的 NSLog 内容

iphone - 更新至 iOS 4.1 且 ipAd "test advertisment"未显示在模拟器上

ios - 如何增加整个应用程序的字体大小?

ios - 将 iphone 设备添加到 Codenameone 证书生成器时的 Apple Id 查询

iphone - 登录到主应用程序

ios - 如何在 objective-c 中使用 "interface"

ios - IOS 7的currentInputMode替代