ios中网络请求缓存
#import <Foundation/Foundation.h>
#import "ASIFormDataRequest.h" @protocol NetWorkdelegate;
@interface JSNetWork : NSObject<ASIHTTPRequestDelegate>
+(JSNetWork *)shareNetWork; -(void)JsNetWordWithConnectId:(int)connectid Body:(NSString *)body PostBody:(NSString *)PostBody Delegate:(id<NetWorkdelegate>)delegate;
@end @protocol NetWorkdelegate <NSObject>
-(void)NetWorkwithConnectId:(int)connectid backstring:(NSString *)aback;
@end @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=;
request.delegate=self;
request.requestMethod=@"POST";
NSArray *Bodyarry=[PostBody componentsSeparatedByString:@"&"];
for(NSString *tmp in Bodyarry){
NSArray *temparrary=[tmp componentsSeparatedByString:@"="];
[request setPostValue:temparrary[] forKey:temparrary[]];
}
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: 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>)
[self NetWorkwithConnectId: backstring:str];
} -(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];
} - (void)dealloc {
[_txt release];
[super dealloc];
}
ios中网络请求缓存的更多相关文章
- ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)
当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...
- iOS开发网络请求——大文件的多线程断点下载
iOS开发中网络请求技术已经是移动app必备技术,而网络中文件传输就是其中重点了.网络文件传输对移动客户端而言主要分为文件的上传和下载.作为开发者从技术角度会将文件分为小文件和大文件.小文件因为文件大 ...
- IOS中http请求使用cookie
http://rainbird.blog.51cto.com/211214/805173 IOS中http请求使用cookie 2012-03-13 23:04:30 标签:http 记录 龙的传人 ...
- 聊聊iOS中网络编程长连接的那些事
1.长连接在iOS开发中的应用 常见的短连接应用场景:一般的App的网络请求都是基于Http1.0进行的,使用的是NSURLConnection.NSURLSession或者是AFNetworking ...
- Java生鲜电商平台-SpringCloud微服务架构中网络请求性能优化与源码解析
Java生鲜电商平台-SpringCloud微服务架构中网络请求性能优化与源码解析 说明:Java生鲜电商平台中,由于服务进行了拆分,很多的业务服务导致了请求的网络延迟与性能消耗,对应的这些问题,我们 ...
- iOS网络——NSURLCache设置网络请求缓存
今天在看HTTP协议,看到了response头中的cache-control,于是就深入的研究了一下.发现了iOS中一个一直被我忽略的类——NSURLCache类. NSURLCache NSURLC ...
- iOS - AFNetworking 网络请求
前言 在 iOS 开发中,一般情况下,简单的向某个 Web 站点简单的页面提交请求并获取服务器的响应,用 Xcode 自带的 NSURLConnection 是能胜任的.但是,在绝大部分下我们所需要访 ...
- iOS - ASIHTTPRequest 网络请求
前言 使用 iOS SDK 中的 HTTP 网络请求 API,相当的复杂,调用很繁琐,ASIHTTPRequest 就是一个对 CFNetwork API 进行了封装,并且使用起来非常简单的一套 AP ...
- iOS - NSURLSession 网络请求
前言 NS_CLASS_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0) @interface NSURLSession : NSObject @available(iOS ...
随机推荐
- Java反编译代码分析(一)
浅析如何读懂这种反编译过来的文件,不喜勿喷. 赋值 Node node; Node node1 = _$3.getChildNodes().item(0); node1; node1; JVM INS ...
- 转:无监督特征学习——Unsupervised feature learning and deep learning
http://blog.csdn.net/abcjennifer/article/details/7804962 无监督学习近年来很热,先后应用于computer vision, audio clas ...
- 在UTF-8中,一个汉字为什么需要三个字节?(转)
http://www.cnblogs.com/web21/p/6092414.html UNICODE是万能编码,包含了所有符号的编码,它规定了所有符号在计算机底层的二进制的表示顺序.有关Unicod ...
- 利用shell脚本批量提交网站404死链给百度
网站运营人员对于死链这个概念一定不陌生,网站的一些数据删除或页面改版等都容易制造死链,影响用户体验不说,过多的死链还会影响到网站的整体权重或排名. 百度站长平台提供的死链提交工具,可将网站存在的死链( ...
- Everything常见问题及搜索技巧,附Demo
1 Everything 1.1 "Everything"是什么? "Everything"是一个运行于Windows系统,基于文件.文件夹名称的快速搜索引擎. ...
- jquery animate()背景色渐变的处理
jquery animate函数不能处理背景色渐变,需要使用jquery.color插件 gitHub地址:https://github.com/jquery/jquery-color/ 使用代码: ...
- 解压版mysql的配置与使用
1.在环境变量path中添加mysql的bin目录路径,例如 D:\Program Files\MySQL\mysql\bin 2.修改mysql目录下的my-default.ini文件 # 设置my ...
- android中实现拨号功能
1.要实现拨号功能,首先需要开启拨号权限 修改AndroidManifest.xml文件,添加如下内容: <uses-permission android:name="android. ...
- 微信小程序 - 使用字体图标(阿里云库)
实现 阿里图库:https://www.iconfont.cn/ CSS3:[attribute*=value] 选择器 步骤 1. 加入购物车 2. 下载代码解压,复制iconfont.css ap ...
- spring boot 运行提示:Process finished with exit code 1
spring boot 运行提示:Process finished with exit code 1 经检查发现是由于在application.properties配置文件中将某些自定义配置项移除了, ...