zoukankan      html  css  js  c++  java
  • Three20 NetWork

    Three20 NetWork  是一个针对NSUrlConnection进行封装的一个强大的网络处理模块,并且实现了强大的缓存机制。

    1. 首先我们先看看 TTURLRequest 这个类, 允许你自定义http method, body and  parameters, as well as natural response.  processing using TTURLResponse objects. 

    #import <Foundation/Foundation.h>
    // Network
    #import "Three20Network/TTURLRequestCachePolicy.h"
    // Core
    #import "Three20Core/TTCorePreprocessorMacros.h"// For __TTDEPRECATED_METHOD
    @protocol TTURLRequestDelegate;
    @protocol TTURLResponse;
    @interface TTURLRequest : NSObject {
      NSString*             _urlPath;             //url路径
      NSString*             _httpMethod;     //http方法
      NSData*               _httpBody;          //http体
      NSMutableDictionary*  _parameters;  //参数
      NSMutableDictionary*  _headers;       //http头
      NSString*             _contentType;           //内容类型
      NSStringEncoding      _charsetForMultipart;  //字符集编码
      NSMutableArray*       _files;                     //上传多个文件
      id<TTURLResponse>     _response;       
      TTURLRequestCachePolicy _cachePolicy;         //缓存代理
      NSTimeInterval          _cacheExpirationAge;        //缓存实效时间,默认是1天
      NSString*               _cacheKey;                               //缓存key
      NSDate*               _timestamp;                                
      NSInteger             _totalBytesLoaded;               //已经加载的字节数      
      NSInteger             _totalBytesExpected;           //预计下载的字节数
      NSInteger             _totalBytesDownloaded;      //下载的总字节数
      NSInteger             _totalContentLength;         //总的内容长度
      id    _userInfo;                                                 //用户信息
      BOOL  _isLoading;                                         //是否正在加载
      BOOL  _shouldHandleCookies;                   //是否处理cooke
      BOOL  _respondedFromCache;                    //是否从缓存里面取
      BOOL  _filterPasswordLogging;                  //记录密码日志
      NSMutableArray* _delegates; 
    }
    /**
     * 请求加载的url路径
     */
    @property (nonatomic, copy) NSString* urlPath;
    /**
     同上,已经不再使用了,用urlPath代替
     */
    @property (nonatomic, copy) NSString* URL__TTDEPRECATED_METHOD;
    /**
     * 请求的http 方法名字
     * @example @"POST"
     * @example @"GET"
     * @example @"PUT"
     * @默认 (t @"GET")
     */
    @property (nonatomic, copy) NSString* httpMethod;
    /* * 一个处理response data 并且可以解析和验证的对象, 下面几个是处理的类型, 其中TTURLXMLResponse需要添加一个扩展框架 extThree20XML
     * @see TTURLDataResponse
     * @see TTURLImageResponse
     * @see TTURLXMLResponse
     */
    @property (nonatomic, retain) id<TTURLResponse> response;
    /**
     *  请求发送的Http  body
     * 如果提供了,就会一直使用.  当你使用POST或put方法的时候 ,先确定下。 
     *  如果httpBody提供了,那么post或put  data 会从parameters属性 里面生成,不会被使用
     */
    @property (nonatomic, retain) NSData* httpBody;
    /**
     * 请求数据的内容类型 The content type of the data in the request.
     * 如果没有提供,并且httpMethod是post或 put ,那么contentType 是@"multipart/form-data".
     */
    @property (nonatomic, copy) NSString* contentType;
    /**
     *  HTTP POST/PUT方法的参数
     */
    @property (nonatomic, readonly) NSMutableDictionary* parameters;
    /**
     * 自定义HTTP headers.
     */
    @property (nonatomic, readonly) NSMutableDictionary* headers;
    /**
     * @默认缓存: TTURLRequestCachePolicyDefault
     */
    @property (nonatomic)TTURLRequestCachePolicy cachePolicy;
    /**
     *  设置response缓存数据的最长时间
     *默认是TT_DEFAULT_CACHE_EXPIRATION_AGE (1 week)
     */
    @property (nonatomic)NSTimeInterval cacheExpirationAge;
    /**
     * 如果没有cache key, 那么唯一的key是从request data生成的。
     *如果请求是post或put,那么post或put参数也会使用cache key生成的。
     *如果设置了cache key ,你可以覆盖默认的cache key,使用你自己的。
     */
    @property (nonatomic, copy) NSString* cacheKey;
    /**
     * 一个虚拟的对象,用来标识请求的唯一对象标识
     *生成TTUserInfo对象会在这里使用
     * @see TTUserInfo
     */
    @property (nonatomic, retain) id userInfo;
    @property (nonatomic, retain) NSDate* timestamp;
    /**
     * 当前请求是否激活了
     */
    @property (nonatomic) BOOL isLoading;
    /**
     * 请求是否使用默认的cookie处理
     * @param YES if cookies should be sent with and set for this request;
     *        otherwise NO.
     * @discussion The default is YES - 相反cookies are sent from and
     *             stored to the cookie manager by default.
     * @default YES
     */
    @property (nonatomic)BOOL shouldHandleCookies;
    /**
     * 请求已经加载的字节数
     */
    @property (nonatomic)NSInteger totalBytesLoaded;
    /**
     * 请求预计要加载的字节数
     */
    @property (nonatomic)NSInteger totalBytesExpected;
    /**
     *已经从服务器上下载的总字节数
     */
    @property (nonatomic)NSInteger totalBytesDownloaded;
    /**
     *  请求的总内容长度
     */
    @property (nonatomic)NSInteger totalContentLength;
    /**
     * 是否从缓存加载
     * 前提是请求已经加载完成
     */
    @property (nonatomic)BOOL respondedFromCache;
    /**
    名字为密码的参数是否要记录日志.
     */
    @property (nonatomic,assign) BOOL filterPasswordLogging;
    /**
     * 当创建multipart/form-data  数据使用的字符集 *
     * @默认是 NSUTF8StringEncoding 
     */
    @property (nonatomic)NSStringEncoding charsetForMultipart;
    /**
     * An array of non-retained objects that receive messages about the progress of the request.
     */
    @property (nonatomic, readonly) NSMutableArray* delegates;
    + (TTURLRequest*)request;
    + (TTURLRequest*)requestWithURL:(NSString*)URL delegate:(id/*<TTURLRequestDelegate>*/)delegate;
    - (id)initWithURL:(NSString*)URL delegate:(id/*<TTURLRequestDelegate>*/)delegate;
    - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
    /**
     *添加一个文件,整个文件数据发送出去
     */
    - (void)addFile:(NSData*)data mimeType:(NSString*)mimeType fileName:(NSString*)fileName;
    /**
     * 尝试发送一个请求
     *如果请求可以通过缓存来处理,就是同步请求
     *   否则这个就是异步请求。
     * @return YES  如果请求从缓存加载的话,就是同步请求
     */
    - (BOOL)send;
    /**
     * 尝试发送一个同步请求
     *不管数据是从网络取还是从缓存取,都会使用同步请求
     */
    - (BOOL)sendSynchronously;
    /**
     * 取消请求
     *如果是多个相同的url请求,其他的不会取消
     */
    - (void)cancel;
    - (NSURLRequest*)createNSURLRequest;
    @end

    先创建一个TTURLRequest 对象  

    TTURLRequest *request  = [TTURLRequestrequestWithURL:kRequestURLPath delegate:self];

    并实现TTURLRequestDelegate协议

    处理图片请求使用: TTURLImageResponse,TTURLImageResponse仅仅是response types里面的一个,你也可以使用 TTURLDataResponse 和 TTURLXMLResponse(这个xmlResponse需要添加一个扩展框架extThree20XML )

    request.response = [[[TTURLImageResponsealloc] init]autorelease];
    
    [request send];  //异步方式去请求
    #pragma mark -
    
    #pragma mark TTURLRequestDelegate
    
    //开始请求
    
    - (void)requestDidStartLoad:(TTURLRequest*)request {
    
      [_requestButtonsetTitle:@"Loading..."forState:UIControlStateNormal];
    
    }
    
    //请求完成
    
    - (void)requestDidFinishLoad:(TTURLRequest*)request {
    
      TTURLImageResponse* imageResponse = (TTURLImageResponse*)request.response;
    
      if (nil == _imageView) {
    
        _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    
        [_scrollView addSubview:_imageView];
    
      }
    
      _imageView.image = imageResponse.image;
    
      [_imageView sizeToFit];
    
      _imageView.alpha = 0;
    
      [UIView beginAnimations:nil context:nil];
    
      [UIView setAnimationDuration:0.5];
    
      _requestButton.alpha = 0;
    
      _clearCacheButton.alpha = 0;
    
      [_scrollView setContentSize:_imageView.frame.size];
    
      _imageView.alpha = 1;
    
      [UIView commitAnimations];
    
    }
    
    //请求错误处理
    
    - (void)request:(TTURLRequest*)request didFailLoadWithError:(NSError*)error {
    
      [_requestButtonsetTitle:@"Failed to load, try again."forState:UIControlStateNormal];
    
    }
  • 相关阅读:
    eclipse写javaee的时候js文件新增函数找不到
    baidu春招题:熊回家
    java自定义容器排序实现接口
    Thread主体和执行主体
    jqurey定位 id
    c中二维数组与指针访问
    ubuntu上浏览器上不了网
    前端经典面试题
    HTML,CSS,JS试题
    CSS3实现文字浮雕效果,镂刻效果,火焰文字
  • 原文地址:https://www.cnblogs.com/limlee/p/2547836.html
Copyright © 2011-2022 走看看