网络请求(get Post)

 

 

---------------------------------------NetworkReques.h文件

 

@interface MainConnection :NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate>

 

@property (nonatomic,strong)NSURLConnection *conn;

@property (nonatomic,strong)NSMutableData *webData;

@property (nonatomic,strong)NSString *parsType;

@property (nonatomic,assign)BOOL isFinished;

@property (nonatomic,assign)BOOL isSumit;

 

/**

 * 发送get请求

 */

- (void)getSendUrl:(NSString *)urlStr;

/**

 * 接口名称

 * 接口参数

 * 发送Post请求

 */

-(void)sendPostRequest:(NSString *)urls HttpBody:(NSString *)part;

 

 

----------------------------------------NetworkReques.m文件

 

-(void)getSendUrl:(NSString *)urlStr{

    urlStr = [NSStringstringWithFormat:@"%@%@",UrlHead,urlStr];

    NSURL *url = [NSURLURLWithString:urlStr];

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];

    [request setTimeoutInterval:10];

    [request setHTTPMethod:@"GET"];

    _conn = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];

    if (_conn) {

        _webData = [NSMutableDatadata];

    }

    _isFinished =NO;

    while (!_isFinished) {

        [[NSRunLoopcurrentRunLoop]runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];

    }

}

-(void)sendPostRequest:(NSString *)urls HttpBody:(NSString *)parm

{

    //获取手机的imsi  46001则为联通

//    NSString *str = [MyTools getIMSI];

    //判断当前网络状况是否是wifi

//    NSInteger stateNum = [IsNetWork isWIFI];

//    if ([str isEqualToString:@"46001"] && stateNum == 2) {

//        urls = [NSString stringWithFormat:@"%@%@",LianTongUrlHead,urls];

//    }else{

        urls = [NSStringstringWithFormat:@"%@%@",UrlHead,urls];

//    }

    NSURL *url = [NSURLURLWithString:urls];

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];

    

    [request setTimeoutInterval:10];

    [request setHTTPMethod:@"POST"];

    

    request.HTTPBody = [parmdataUsingEncoding:NSUTF8StringEncoding];

    

    

    

    _conn = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];

    

    if (_conn) {

        _webData = [NSMutableDatadata];

    }

    _isFinished =NO;

    while (!_isFinished) {

        [[NSRunLoopcurrentRunLoop]runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];

    }

    

}

 

#pragma mark - detegate

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    

    [_webData appendData:data];

    WBLog(@"%@",_webData);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    WBLog(@"%@",error);

 

    if (_isSumit == YES) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MainConnection" object:nil userInfo:@{@"linkServer":@"PostLink"}];
    }else{
         [[NSNotificationCenter defaultCenter] postNotificationName:@"MainConnection" object:nil userInfo:@{@"linkServer":@"GetLink"}];
    }

    [MyToolscreateMyAlertView:nilandTitle:@"无法连接到服务器,请求超时,稍后再试!"];

}

 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    _isFinished =YES;

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    [_webDatasetLength:0];

}

 

 

 

调用

//全局参数 

     NetworkReques *post;

 

 

     BOOL isSubmitBool ;

- (void)viewDidLoad {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClickSign:) name:@"MainConnection" object:nil];
    
}

-(void)onClickSign:(NSNotification *)notifier{
    NSString *str = [notifier.userInfo objectForKey:@"linkServer"];
    if ([str isEqualToString:@"PostLink"]) {
        isSubmit = NO;
    }
    self.act.hidesWhenStopped = YES;
    [self.act stopAnimating];
}

 

 

-(void)onClickSubmit{

   if(isSubmitBool== YES){

 

     return;

   }

   isSubmitBool==YES;

   NSString *urlStr = [NSStringstringWithFormat:@"IOS_InsertTrip"];

   NSString *parmStr = [NSStringstringWithFormat:kIOS_InsertTrip,

                                delegate.SellerCode,

                                dataSource[1],

                                dataSource[0],

                                dataSource[2],

                                dataSource[4],

                                mp3str1,

                                dataSource[3]];

           

   [selfsendPostRequest:urlStr andHttpBody:parmStr andUrlType:@"tijiao"];

 

}

 

 

#pragma mark - SendPostRequest

-(void)sendPostRequest:(NSString *)url andHttpBody:(NSString *)parm andUrlType:(NSString *)urlType{

    

    post = [[NetworkRequesalloc] init];

    post.parsType = urlType;

    [post sendPostRequest:url HttpBody:parm];

    post.isSubmit = YES;

    

    

    if (post.isFinished ==YES) {

        if ([post.parsType isEqualToString:@"tijiao"]) {

            [selfparesDataObject:urlType];

        }

    }else{

        isSubmitBool =NO;

    }

}

- (void)paresDataObject:(NSString *)urlType{

    NSString *str = [[NSStringalloc] initWithData: post.webData encoding:NSUTF8StringEncoding];

    NSArray *array = [strcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];

    NSString *str2 = [arrayobjectAtIndex:4];

    

    if ([urlTypeisEqualToString:@"tijiao"]) {

        if ([str2isEqualToString:@"true"]) {

            [MyToolscreateMyAlert:selfandTitle:@"提交成功!"];

        }else{

            isSubmitBool =NO;

            [MyToolsMBProgressHUD:selfandContent:@"提交失败!"];

        }

    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值