NSURLConnection没有回应 [英] NSURLConnection not responding

查看:95
本文介绍了NSURLConnection没有回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从URL获取一些数据,但出于某种原因,当我执行以下操作时没有任何反应。 didReceiveResponse: didReceiveData: didFailWithError:或<达到code> connectionDidFinishLoading:,除非我通过这样做为我的请求添加超时: [request setTimeoutInterval:10.0]

I'm trying to get some data from an URL, but for some reason, nothing happens when I do the following. Neither didReceiveResponse:, didReceiveData:, didFailWithError: or connectionDidFinishLoading: are reached, except when I add a timeout to my request by doing this: [request setTimeoutInterval:10.0]

这就是我正在做的事情:

Here's what I'm doing :

-(void)getConfigFromServer{
    [self getContentAtURL:kUrlGetUser];
}

//Va chercher le contenu à l'URL passée en paramètre
- (void)getContentAtURL: (NSURL *)url {

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString * userLogin = [userDefaults objectForKey:@"UserLogin"];
    NSString * userPassword = [userDefaults objectForKey:@"UserPassword"];
    NSURL * urlFinal = [NSURL URLWithString:[NSString stringWithFormat:@"%@", url]];
    NSLog(@"Request : %@", [NSString stringWithFormat:@"%@", url]);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:urlFinal];  
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:10.0];

    NSString *sourceString = [NSString stringWithFormat:@"%@:%@", userLogin, userPassword]; 
    NSData * sourceData = [sourceString dataUsingEncoding:NSUTF8StringEncoding];  
    NSString *authString = [sourceData base64EncodedString];  

    authString = [NSString stringWithFormat: @"Basic %@", authString];
    [request setValue:authString forHTTPHeaderField:@"Authorization"];
    [request setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
    NSURLConnection * connection=[NSURLConnection connectionWithRequest:request delegate:self];

    if(connection){
        NSLog(@"Connection started");
        receivedData = [NSMutableData data];
    }else{
        NSLog(@"Error while trying to initiate the connection");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0];
    if ([response respondsToSelector:@selector(statusCode)])
    {
        int statusCode = [((NSHTTPURLResponse *)response) statusCode];
        if (statusCode >= 400)
        {
            [connection cancel];  // stop connecting; no more delegate messages
            NSDictionary *errorInfo
                = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:
                NSLocalizedString(@"Server returned status code %d",@""),
                statusCode]
                    forKey:NSLocalizedDescriptionKey];
            NSError *statusError = [NSError errorWithDomain:@"Error"
                code:statusCode
                userInfo:errorInfo];
            [self connection:connection didFailWithError:statusError];
        }
    }
}

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

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

编辑:我还有这个问题,现在有了也是实际的设备。正如我在评论中所说,我第一次在这个应用程序上使用ARC,我正在使用XCode 4.2。

EDIT : I'm still having this problem, and now have it on the actual device too. As I said it in the comments, I use ARC on this app for the first time, and I'm using XCode 4.2.

任何想法?

推荐答案

这是旧帖子,但这个答案仍然可以帮助那些人。

This is old thread but still this answer might help someone out there.

如果你在后台线程中调用它,请在调用代理之前检查线程是否正在退出。

If you are calling this in background thread, check whether you thread is exiting before delegates is called.

试试这个。

NSURLConnection * connection = [[NSURLConnection alloc] 
                                initWithRequest:request
                                       delegate:self startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                      forMode:NSDefaultRunLoopMode];
[connection start];

这篇关于NSURLConnection没有回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆