NSURLConnection的Asyncronous请求返回null [英] NSURLConnection Asyncronous Request returns null

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

问题描述

其次最近的YouTube教程尝试连接到网址异步。
重调空的响应,但数据长度大于0我见过的其他code,做两个空&放大器;长度检查,我没扔,只是NSLog'd他们。

Followed a recently made YouTube tutorial try connecting to web address asynchronously. Retuning null for the response, but the data length is greater than 0. I've seen other code that does both null & length checking, which I didn't throw, just NSLog'd them.

@implementation ViewController

-(void)fetchAddress:(NSString *)address  {

NSLog(@"Loading Address: %@", address);
[iOSRequest requestToPath:address onCompletion:^(NSString *result, NSError *error)  {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (error)  {
            [self stopFetching:@"Failed to Fetch"];
            NSLog(@"%@", error);
        } else  {
            [self stopFetching:result];
            NSLog(@"Good fetch:  %@", result);
        }
        });
    }];

}


- (IBAction)fetch:(id)sender {

    [self startFetching];
    [self fetchAddress:self.addressField.text];
    NSLog(@"Address: %@", self.addressField.text);

}


-(void)startFetching  {

    NSLog(@"Fetching...");
    [self.addressField resignFirstResponder];
    [self.loading startAnimating];
    self.fetchButton.enabled = NO;

}


-(void)stopFetching:(NSString *)result  {

    NSLog(@"Done Fetching  %@", result);
    self.outputLabel.text = result;
    [self.loading stopAnimating];
    self.fetchButton.enabled = YES;

}




@implementation iOSRequest


    +(void)requestToPath:(NSString *)
        path onCompletion:(RequestCompletionHandler)complete  {


    NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];


    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]
        cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
        timeoutInterval:10];


    NSLog(@"path:  %@  %@", path, request);

    [NSURLConnection sendAsynchronousRequest:request
        queue:backgroundQueue
        completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)  {
        NSString *result = [[NSString alloc] initWithData:
            data encoding:NSUTF8StringEncoding];
        if (complete)  {
           complete(result, error);
           NSLog(@"result:  %@ %lu %@", result, (unsigned long)[data length], error);
        }
    }];
}

请求http://www.google.com>
响应为空
数据长度为13644

request is http://www.google.com> response is null data length is 13644

不知道什么是错的...有什么建议?

Not sure what is wrong... any suggestions?

推荐答案

感谢约什 - 卡斯威尔的指点方向是正确的,但让我找出自己。

Thanks to Josh Caswell for pointing in the right direction, but allowing me to figure it out myself.

改变了initWithData编码从NSUTF8StringEncoding到NSASCIIStringEncoding。

Changed the initWithData encoding from NSUTF8StringEncoding to NSASCIIStringEncoding.

[NSURLConnection sendAsynchronousRequest:request
    queue:backgroundQueue
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)  {
    NSString *result = [[NSString alloc] initWithData:
        data encoding:NSASCIIStringEncoding];
    if (complete)  {
       complete(result, error);
       NSLog(@"result:  %@ %lu %@", result, (unsigned long)[data length], error);
    }
}];

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

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