iOS url

来源:互联网 发布:生鲜配送软件 编辑:程序博客网 时间:2024/05/23 15:44

An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data. 指明获取数据的对象  网站地址 或者文件地址等

NSURLRequest objects represent a URL load request in a manner independent of protocol and URL scheme.创建一个请求,可以指定缓存策略以及超时时间。


 缓存类型
      NSURLRequestUseProtocolCachePolicy        默认的缓存策略,是最能保持一致性的缓存策略
      NSURLRequestReloadIgnoringCacheData     忽略缓存直接从原始地址下载
      NSURLRequestReturnCacheDataElseLoad    只有在缓存中不在data时才从原始地址下载
      NSURLRequestReturnCacheDataDontLoad   准许app确定是否要返回cache数据,如果使用这种协议当本地不存在response的时候,创建NSURLConnextion or NSURLDownload  实例时将会马上返回nil;类似于离线模式,没有建立网络连接

The NSURLConnection class provides convenience class methods to load URL requests both asynchronously using a callback block and synchronously.连接成功则返回数据

   NSURLConnection的主要方法包括:

  1.  start:开始下载。默认初始化设定了delegate后便自动开始,但如果没有设定则可以调用此开始 
  2. cancel:在一次请求开始到结束之间调用cancel,会结束当前的请求,并不再接受消息。   

     NSURLConnection的代理方法有:

  1. connection :didReceiveResponse:当接受了足够创建NSURLResponse对象的数据后会调用。
  2. connection:didReceiveData:下载开始后,每当有数据包,都会定期的调用此方法,我们应该在该方法里存储接受到的数据。
  3. connection:didFailWithError:下载过程中有异常,返回失败信息后便不再接收数据
  4. connectionDidFinishLoading:下载顺利完成。
   
    实际测试中发现的问题:Get方式可以很好的执行time out,但POST方式却不能相应time out时间。查阅资料发现Post的最短time out时间为240秒,如果你setTimeoutInterval小于240,则会被忽略。因此我们只能通过注册自己的Timer,当超过时间后调用cancel。

For greater control, you can create a URL connection object with a delegate object that conforms to the NSURLConnectionDelegate andNSURLConnectionDataDelegate protocols. The connection calls methods on that delegate to provide you with progress and status as the URL request is loaded asynchronously. The connection also calls delegate methods to let you override the connection’s default behavior (for example, specifying how a particular redirect should be handled). These delegate methods are called on the thread that initiated the asynchronous load operation.



NSJSONSerialization


NSJSONReadingOptions

Options used when creating Foundation objects from JSON data—see JSONObjectWithData:options:error: and JSONObjectWithStream:options:error:.

enum {   NSJSONReadingMutableContainers = (1UL << 0),   NSJSONReadingMutableLeaves = (1UL << 1),   NSJSONReadingAllowFragments = (1UL << 2)};typedef NSUInteger NSJSONReadingOptions;
Constants
NSJSONReadingMutableContainers

Specifies that arrays and dictionaries are created as mutable objects.// 应该是最灵活的 所有对象都可以

Available in iOS 5.0 and later.

Declared in NSJSONSerialization.h.

NSJSONReadingMutableLeaves

Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.//可变字符串对象

Available in iOS 5.0 and later.

Declared in NSJSONSerialization.h.

NSJSONReadingAllowFragments

Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary.//不能是不可变的对象

Available in iOS 5.0 and later.

Declared in NSJSONSerialization.h.


0 0
原创粉丝点击