使用sendAsynchronousRequest:queue:completionHandler:进行URL请求 [英] Using sendAsynchronousRequest:queue:completionHandler: for URL request

查看:88
本文介绍了使用sendAsynchronousRequest:queue:completionHandler:进行URL请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发另一个开发人员创建的iOS应用程序的更新.他使用ASIHTTPRequest处理http请求.但是,我必须使用的应用程序版本崩溃.由于ASIHTTPRequest不再进行更新,所以我当时以为应该切换到使用AFNetworking,但是要弄清楚它太复杂了.

I'm working on an update of an iOS app that another developer created. He was using ASIHTTPRequest to handle http requests. However, the version of the app I have to work with crashes. Since ASIHTTPRequest is not being updated anymore, I thought then that I should switch to using AFNetworking, but it's far too complicated for me to figure out.

所以最后我以为我可以单独使用NSURLConnection.

So finally I thought I could just use NSURLConnection on its own.

这是我要替换的原始代码:

Here is the original code that I'm trying to replace:

-(NSArray*)readUTF16LEFeed:(NSURL*) urlToRead{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlToRead];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        lastModified = [NSDate date];
        NSData *response = [request responseData];//UTF-16LE
        NSString* responseString = [[NSString alloc] initWithData:response encoding:NSUTF16LittleEndianStringEncoding];
        DLog(@"string is: %@",responseString);
        responseString = [responseString stringByReplacingOccurrencesOfString:@"ISO-8859-1" withString:@"UTF16-LE"];
        NSData* data = [responseString dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
        return [self parseNamesFromXML:data];
    }
    return nil;
}

这就是我要使用的:

-(NSArray*)readUTF16LEFeed:(NSURL*) urlToRead{

    NSURLRequest *request = [NSURLRequest requestWithURL:urlToRead];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error ) {

                               if ([data length] >0 && error == nil)
                               {
                                   // DO YOUR WORK HERE
                                   lastModified = [NSDate date];

                                   //   NSData *response = [request responseData];//UTF-16LE
                                   NSString* responseString = [[NSString alloc] initWithData:data encoding: NSUTF16LittleEndianStringEncoding];
                                   DLog(@"string is: %@",responseString);
                                   responseString = [responseString stringByReplacingOccurrencesOfString:@"ISO-8859-1" withString:@"UTF16-LE"];
                                   NSData* data = [responseString dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
                                   return [self parseNamesFromXML:data];
                               }

                               else if ([data length] == 0 && error == nil)
                               {
                                   NSLog(@"Nothing was downloaded.");
                               }
                               else if (error != nil){
                                   NSLog(@"Error = %@", error);
                               }
                           }];
    return nil;
}

但是,我收到错误消息将NSArray发送到类型为void的参数的不兼容块指针类型.而且,控件可能到达非空块的末尾."

However, I'm getting the error "Incompatible block pointer types sending NSArray to parameter of type void. And also "Control may reach end of non-void block."

如何使它正常工作?

推荐答案

使方法的返回类型为void.不要退货.只需调用[self parseNamesFromXML:data];方法.

Make return type of your method as void. Dont return anything. Just call [self parseNamesFromXML:data]; method.

-(void)readUTF16LEFeed:(NSURL*) urlToRead{


NSURLRequest *request = [NSURLRequest requestWithURL:urlToRead];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error ) {

                       if ([data length] >0 && error == nil)
                       {

                           // DO YOUR WORK HERE
                           lastModified = [NSDate date];

                      //   NSData *response = [request responseData];//UTF-16LE
                           NSString* responseString = [[NSString alloc] initWithData:data encoding: NSUTF16LittleEndianStringEncoding];
                           DLog(@"string is: %@",responseString);
                           responseString = [responseString stringByReplacingOccurrencesOfString:@"ISO-8859-1" withString:@"UTF16-LE"];
                           NSData* data = [responseString dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
                           [self parseNamesFromXML:data];
                       }


                       else if ([data length] == 0 && error == nil)
                       {
                           NSLog(@"Nothing was downloaded.");
                       }
                       else if (error != nil){
                           NSLog(@"Error = %@", error);
                       }

                   }];
}

parseNamesFromXML方法中,处理结果并将结果数组分配给属性.您可以随时随地访问它.

In parseNamesFromXML method, process your results and assign results array to a property. You can access it where ever you want.

-(void) parseNamesFromXML:(NSData *)xmlData
{     
    NSError *error;          
    //DLog(@"data is %@", [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding ]);     

    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData                                                            options:0 error:&error];          

    if (doc)
    {         
        self.dataArray = [self parseXmlDoc:doc]; 
    }              
}

这篇关于使用sendAsynchronousRequest:queue:completionHandler:进行URL请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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