zoukankan      html  css  js  c++  java
  • 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=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)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];
    }
  • 相关阅读:
    第九十一天 how can I 坚持 技术-永远的技术
    第九十天 how can I 坚持
    Java控制台中输入中文输出乱码的解决办法
    【体系结构】转移预测器设计与比较1
    Ubuntu 13.04 用Sublime Text 2 编译运行 JAVA
    HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)
    个人重构机房收费系统之报表
    快速排序的递归和非递归实现
    HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest)
    体验决定深度,知识决定广度。你的人生是什么呢? 操蛋和扯蛋没必要纠结 唯有继续
  • 原文地址:https://www.cnblogs.com/gcb999/p/3226569.html
Copyright © 2011-2022 走看看