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;                               //缓存keyNSDate*               _timestamp;                                NSInteger             _totalBytesLoaded;               //已经加载的字节数      NSInteger             _totalBytesExpected;           //预计下载的字节数NSInteger             _totalBytesDownloaded;      //下载的总字节数NSInteger             _totalContentLength;         //总的内容长度id    _userInfo;                                                 //用户信息BOOL  _isLoading;                                         //是否正在加载BOOL  _shouldHandleCookies;                   //是否处理cookeBOOL  _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];}

转载于:https://www.cnblogs.com/limlee/archive/2012/06/13/2547836.html

Three20 NetWork相关推荐

  1. iphone three20 保存本地的图片

    1:http://www.cnblogs.com/innhyul/archive/2010/11/13/1876583.html 由于开发需要,准备做一个图片浏览器,用来显示已经下载存处在本地的图片. ...

  2. Distilling the Knowledge in a Neural Network 论文笔记蒸馏

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/bryant_meng/article/ ...

  3. CentOS7 service network start命令启动时报错解决方法

    service network start命令启动时报错: [root@localhost network-scripts]# service network start Starting netwo ...

  4. 面向Mobile device的CNN模型手工设计与NAS分析总结,MobileNet V1,V2,V3,Efficient,MNasNet以及Efficient network design

    手工方法和NAS的高效网络模型设计总结与分析 这篇文章主要关注对于移动端,资源受限平台的高效神经网络设计(Manually)和搜索(NAS). ​​​​​​高效的CNN设计不只是用在服务器,云端,资源 ...

  5. 基于caffe的度量学习实现(Siamese network Triplet network)

    基于caffe的度量学习实现,主要是孪生网络和三元组网络(Siamese network & Triplet network)实现图像的分类和度量. 包含数据集制作脚本,训练测试脚本和pyth ...

  6. Corner Proposal Network 论文阅读(2020ECCV)

    Introduction 引言 介绍了anchor base 和 anchor free 与one stage 和 two stage ,然后说通常认为的anchor base 有低召回的问题,anc ...

  7. 创建第一个 local network(I) - 每天5分钟玩转 OpenStack(80)

    在 ML2 配置文件中 enable local network 后,本节将开始创建第一个 local network. 我们将通过 Web GUI 创建第一个 local network. 首先确保 ...

  8. 【Qt】Qt中使用ssl时报错:qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method

    一.问题 在Qt中使用https,运行时报错: qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method 二.原因分析 SSLv2由 ...

  9. POJ 1236 Network of Schools(tarjan)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

最新文章

  1. Hook技术之4 在自己的进程中注入一个Dll到别人的进程
  2. Skia的SkCamera.cpp的doUpdate() 算法。
  3. vue中checkbox 样式自定义重写;循环遍历checkbox,拿到不同的v-model绑定值;及获取当前checked 状态,全选和全不选等功能。...
  4. java工程师考试题目_成功拿到Offer,Java工程师笔试题及答案!
  5. Android之提示Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider
  6. 借百度爸爸下蛋的好看搜索框
  7. 在java中8421_JAVA常量介绍
  8. .NET 开发系统 -知识 点
  9. python安装xlrd和xlwt及应用
  10. GitLab的CICD配置文件.gitlab-ci.yml
  11. oracle中ipad是什么意思,oracle 函数 Ipad的简单用法
  12. 什么软件可以查手机卡的imsi_怎么查看手机的IMSI?
  13. 功率是电压电流乘积的波形在一个周期内积分后除以周期。
  14. 数据挖掘、数据分析以及大数据之间的区别有哪些?
  15. 股票成交量和价格关系
  16. 条件运算符 JAVA
  17. 海康摄像机3D PTZ功能,拍照功能
  18. 浙大版《C语言程序设计》第四版(何钦铭颜晖) 第12章 文件 课后习题答案
  19. 造车失败后投身机器人和AI,我笑戴森太疯癫,戴森笑我看不穿
  20. 线性代数复习归纳(一):矩阵+例题

热门文章

  1. Vue.js 第二天: 事件处理
  2. 用Python处理Excel文件
  3. ZenHub Epics创造了GitHub中敏捷Epics
  4. Lync Server 2010企业版系列PART1:基础构建
  5. Apache2 httpd.conf中文版
  6. Gartner 2018 年WAF魔力象限报告:云WAF持续增长,Bot管理与API安全拥有未来
  7. 保持Service不被Kill掉的方法--双Service守护 Android实现双进程守护 2
  8. 使用servlet+jdbc+MD5实现用户加密登录
  9. 修炼Python基础篇-set学习
  10. ESXI上的新建虚机绑定已使用过的静态ip无法ping通网关的奇怪现象