ASIHTTPRequest的使用——网络请求缓存
2014-05-16 09:15
489 查看
///////////////////////////////////////////////////////////////////////////
.m文件
///////////////////////////////////////////////////////////////////////////
使用文件
///////////////////////////////////////////////////////////////////////////
.h文件 #import <Foundation/Foundation.h> #import "ASIFormDataRequest.h" @protocol NetWorkdelegate <NSObject> - (void)NetWorkwithConnectId:(int)connectid backstring:(NSString *)aback; @end @interface JSNetWork : NSObject <ASIHTTPRequestDelegate> + (JSNetWork *)shareNetWork; - (void)JsNetWordWithConnectId:(int)connectid Body:(NSString *)body PostBody:(NSString *)PostBody Delegate:(id<NetWorkdelegate>)delegate; @end
.m文件
@implementation JSNetWork + (JSNetWork *)shareNetWork { static dispatch_once_t onceToken; static JSNetWork *netWork = nil; dispatch_once(&onceToken, ^{ netWork = [[JSNetWork alloc] init]; }); return netWork; } #pragma mark -request - (void)JsNetWordWithConnectId:(int)connectid Body:(NSString *)body PostBody:(NSString *)PostBody Delegate:(id<NetWorkdelegate>)delegate { if (![self Isconnect]) { UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:nil message:@"not work" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; [alertview show]; [alertview release]; return; } ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:body]]; request.timeOutSeconds = 30; request.delegate = self; request.requestMethod = @"POST"; NSArray *Bodyarry = [PostBody componentsSeparatedByString:@"&"]; for (NSString *tmp in Bodyarry) { NSArray *temparrary = [tmp componentsSeparatedByString:@"="]; [request setPostValue:temparrary[1] forKey:temparrary[0]]; } NSString *cid = [NSString stringWithFormat:@"%zi",connectid]; NSDictionary *dic = @{@"connectid":cid,@"delegate":delegate}; [request setUserInfo:dic]; [request startAsynchronous]; } - (void)requestFinished:(ASIHTTPRequest *)request { NSDictionary *userinfo = request.userInfo; int connectid = [userinfo[@"connectid"] intValue]; id<NetWorkdelegate> delegate = userinfo[@"delegate"]; NSString *str = [request responseString]; [delegate NetWorkwithConnectId:connectid backstring:str]; } - (void)requestFailed:(ASIHTTPRequest *)request { } - (BOOL)Isconnect { BOOL iscon = NO; Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"]; switch ([r currentReachabilityStatus]) { case NotReachable: { NSLog(@"not network"); iscon = NO; } break; case ReachableViaWiFi: { NSLog(@"wifi"); iscon = YES; } break; case ReachableViaWWAN: { NSLog(@"3g"); iscon = YES; } break; default: break; } return iscon; } @end
///////////////////////////////////////////////////////////////////////////
使用文件
- (void)viewDidLoad { [super viewDidLoad]; [[JSNetWork shareNetWork] JsNetWordWithConnectId:100 Body:@"http://www.baidu.com" PostBody:nil Delegate:self]; // Do any additional setup after loading the view, typically from a nib. NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; path = [path stringByAppendingPathComponent:@"1.plist"]; NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; if(str.length > 0) { [self NetWorkwithConnectId:100 backstring:str]; } } - (void)dealloc { [_txt release]; [super dealloc]; } - (void)NetWorkwithConnectId:(int)connectid backstring:(NSString *)aback { self.txt.text = aback; [self Save:aback]; } - (void)Save:(NSString *)str { NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; path = [path stringByAppendingPathComponent:@"1.plist"]; [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; }
///////////////////////////////////////////////////////////////////////////
相关文章推荐
- ASIHttpRequest网络请求第三方类库使用方法详解
- 如何使用ASIHTTPRequest来进行网络请求的
- ASIHTTPRequest使用第三方库处理网络请求
- 归纳笔记013:第三方网络请求ASIHTTPRequest的使用
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iOS网络编程-ASIHTTPRequest框架同步请求
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列
- ASIHTTPRequest 网络请求 iphone
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列
- iOS 使用ASIHttpRequest和GData解析xml网络文件
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列
- 网络之ASIHttpRequest和SBJson简单使用 .
- iOS网络编程-ASIHTTPRequest框架同步请求 推荐
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列
- 使用ASIHTTPRequest进行http 请求bug
- iOS网络编程-ASIHTTPRequest异步请求
- iOS网络编程-ASIHTTPRequest异步请求
- 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
- iOS网络编程-ASIHTTPRequest异步请求
- iOS网络编程-ASIHTTPRequest小例子-数据请求队列