什么是NSURLConnection的替代分块传输编码 [英] What are alternatives to NSURLConnection for chunked transfer encoding

查看:126
本文介绍了什么是NSURLConnection的替代分块传输编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了与此相关的其他问题,但唯一的答案是使用 ASIHTTPRequest ,因为这已经不再开发了我想问什么替代人在使用我们的SDK时,我在服务器接收数据时遇到了很多奇怪的行为 NSURLConnection 。

I've checked for other questions relevant to this, but the only answer is "Use ASIHTTPRequest" as this is no longer being developed I wanted to ask what alternatives people are using, whilst working on our SDK I came across a lot of strange behaviour in NSURLConnection when receiving data from the server.

我们追踪到 NSURLConnection 与chunked-encoding中的响应不一致的事实。或者至少我们在这里读到这个问题 NSURLConnection和chunked转移编码

We tracked it down to the fact that NSURLConnection doesn't deal well with responses in chunked-encoding. Or at least so we read in this question here NSURLConnection and "chunked" transfer-coding

我们正在谈论的一些开发人员说它在iOS 5中变得更好,我们需要确保我们的SDK向后兼容iOS 4.3至少。

Some developers we were talking to say it gets better in iOS 5, we need to make sure that our SDK is backwards compatible with iOS 4.3 at least.

我想确认这是 NSURLConnection 中的一个问题,以及人们如何处理它。

I want to confirm this is infact an issue in NSURLConnection, and how people are dealing with it.

到目前为止,我发现的所有替代品都是基于 NSURLConnection 而我假设是这样的会有同样的缺陷。 ASIHTTPRequest 事实上确实有效,因为它的基础略低于 NSURLConnection ,但他们正在寻找知识中的替代品支持更长时间。

All the alternatives I've found so far are based off of NSURLConnection and I'm assuming as such will have the same flaw. ASIHTTPRequest did in fact work because it's based a little lower than NSURLConnection, but were looking for alternatives in the knowledge it's no longer supported.

其他库的列表是:
Restkit
ShareKit
LRResty
AFNetworking
TTURLRequest

A list of other libraries looked at are: Restkit, ShareKit, LRResty, AFNetworking, TTURLRequest

我知道这里有类似的问题 RESTKit是ASIHTTPRequest的一个很好的替代品吗?和这里ASIHTTPRequest备选方案但这两个解决方案都基于NSURLConnection。

I'm aware there are similar questions here Is RESTKit a good replacement for ASIHTTPRequest? and here ASIHTTPRequest alternative But both of the solutions are based off NSURLConnection.

编辑:我注意到我指出了我的帖子开头的错误问题,所以更新。它指向了2008年的一个帖子,我看到了类似的但最近都没有。

I noticed I pointed to the wrong question at the start of my post, so thats updated. It points to a thread from 2008, and i've seen similar ones but none that are recent.

推荐答案

支持分块传输通过NSURLConnection。我用它们。

Chunked transfers are supported by NSURLConnection. I use them.


  1. 定义一些道具:

  1. Define some props:


NSMutableData * responseData;
NSURLConnection * connection;



  • 建立连接

  • Establish a connection


    NSURL *url = [NSURL URLWithString:@"...."];
    self.responseData = [[NSMutableData alloc] initWithLength:0] ;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    



  • 注册建立连接的回调方法

  • Register your callback method for connection established


    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
         // You may have received an HTTP 200 here, or not...
         [responseData setLength:0];
    }
    



  • 注册你的回调方法chunk received

  • Register your callback method for "chunk received"


    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSString* aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
        NSLog(@"This is my first chunk %@", aStr);
    
    }
    



  • 注册你的连接完成回调:

  • Register your "connection finished" callback:


    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
       [connection release];
    }
    



  • 最后,注册你连接失败回调:

  • And finally, register you "connection failed" callback:




    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        NSLog(@"Something went wrong...");
    }
    


    这篇关于什么是NSURLConnection的替代分块传输编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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