NSURLConnection不返回数据

时间:2014-02-05 22:31:49

标签: objective-c nsurlconnection

我有一个NSURLConnection已经工作了一段时间,突然间无法正常工作。

由于某种原因,被调用的唯一委托方法是:

 -(void)connection:(NSURLConnection *)connection didReceiveResponse:

不会调用其他任何委托方法。我有其他类使用几乎相同的代码,只是一个不同的请求和网址,他们似乎都工作正常。我已经阅读了很多关于确保连接与委托在同一个线程上的帖子,但似乎没有什么对我有用。我知道服务器正在返回响应,因为如果我通过一个简单的html表单传递相同的信息,我在浏览器中得到一个响应,我可以看到我的服务器端脚本正在运行的证据,因为我可以看到它所做的更改SQL数据。应用程序正在获得某种共鸣,它只是没有获取任何数据或调用connectionDidFinishLoading委托方法。

关于问题可能是什么的任何想法?

以下是我的代码的简化版本:

#import "RegistrationViewController.h"

@interface RegistrationViewController ()

@end

NSMutableData *responseData;
NSURLConnection *theconnection;

@implementation RegistrationViewController
/*
 *  Attempt to Register online. Returns False if valiation failed
 */
- (BOOL) registerOnline{

    // OTHER CODE HERE TO BUILD DATA FOR THE REQUEST

    // URL Request
    NSURL *requestUrl = [NSURL URLWithString:THEURL];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:requestUrl];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];


    // Initialse Response Object
    responseData = [[NSMutableData alloc] init];

    // Conection
    theconnection = [NSURLConnection connectionWithRequest:request delegate:self];
    [theconnection start];

    return YES;
}

/*
 * Handle the event of registration failing
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"didFailWithError");

    // OTHER CODE HERE TO HANDLE THE ERROR....

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"did receive response ");

}

/*
 * Handle the reciept of Data
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"didReceiveData");

    // Add data to the response
    [responseData appendData:data];\
}

/*
 * Data finnished loading
 */
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");

    // OTHER CODE HERE TO HANDLE THE RESPONSE....
}

2 个答案:

答案 0 :(得分:0)

您应该查看响应并诊断正在发生的事情。例如,如果statusCode不是200,则可能会出现问题。请参阅HTTP/1.1 Status Code Definitions

因此,您可以查看statusCode

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

        if (statusCode == 200) {
            NSLog(@"received successful (200) response ");
        } else {
            NSLog(@"whoops, something wrong, received status code of %d", statusCode);
        }
    } else {
        NSLog(@"Not a HTTP response");
    }
}

你也在打电话:

theconnection = [NSURLConnection connectionWithRequest:request delegate:self];

自动启动连接。致电

[theconnection start];
你已经第二次开始了。删除start方法,或使用initWithRequest:delegate:startImmediately: NO作为最终参数。

答案 1 :(得分:0)

如果它可以帮助任何人,对我来说问题是我在另一个线程中启动连接

所以一定要打电话

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

theconnection = [NSURLConnection connectionWithRequest:request delegate:self];
[theconnection start];

在主线程中。