ASI网络请求,同步、异步

异步请求:

第一步:实现ASIHTTPRequestDelegate协议

第二步:发送请求
requestGoodsWq = [ASIFormDataRequest requestWithURL:goodsInfo_Url];
    [requestGoodsWq setRequestMethod:@"POST"];
    NSString *uid = [LTCustomerFactory getUUID];
    [requestGoodsWq setPostValue:uid forKey:@"deviceID"];
    [requestGoodsWq setPostValue:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [requestGoodsWq setPostValue:[userDefault objectForKey:UserId] forKey:@"userID"];
    [requestGoodsWq setPostValue:[NowDate getMD5string] forKey:@"ver_code"];
    
    
    requestGoodsWq.delegate = self;
    //    [requestGoodsInfo setPostValue:@"1" forKey:@"goodID"];
    [requestGoodsWq startAsynchronous];
第三步:实现协议
//请求完成协议

- (void)requestFinished:(ASIHTTPRequest *)_request
{
    
    self.view.backgroundColor = [UIColor blackColor];
    [_goodsModelMutableArray removeAllObjects];
    if (_request == requestGoodsInfo) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_request.responseData options:NSJSONReadingMutableContainers error:nil];
        if (dict) {
            NSArray *array = [dict objectForKey:@"goods"];
            
            //解析json
            for (NSDictionary *subdict in array) {
                GoodsModel *goodsModel = [[GoodsModel alloc] initWithDictionary:subdict];
                /*goodsModel.goods_Id = [subdict objectForKey:@"goods_Id"];;
                 goodsModel.goods_imageUrl = [subdict objectForKey:@"goods_imageUrl"];
                 goodsModel.goods_name = [subdict objectForKey:@"goods_name"];
                 goodsModel.goods_info = [subdict objectForKey:@"goods_info"];*/
                //            [_goodsModelMutableArray insertObject:_goodsModel atIndex:page];
                [_goodsModelMutableArray addObject:goodsModel];
                if (![goodsModel.shareUrl isKindOfClass:[NSNull class]]) {
                    [shareUrlArray addObject:goodsModel.shareUrl];
                }
                if (![goodsModel.shareImageUrl isKindOfClass:[NSNull class]]) {
                    [shareImageUrlArray addObject:goodsModel.shareImageUrl];
                }
                
            }
            [self refreshGoods];
        }
        
       
        
        
    }
}
//请求失败协议
<pre name="code" class="objc">- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"请求失败");
    if ([CheckoutNetwork isConnectionNetwork]) {
        if (request == requestGoodsInfo) {
            
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请求失败"
                                                           message:@"请求超时,您要继续请求吗"
                                                          delegate:self
                                                 cancelButtonTitle:@"取消"
                                                 otherButtonTitles:@"确定", nil];
            alert.tag = 10;
            [alert show];
            
        }
        else if (request == requestGoodsWq)
        {
            //        [self _initCommentData:0];
        }
    }
}


 
 


同步请求以及json解析:

-(NSMutableArray*)_loadDataRequest{
    _requestMylikeInfoData = [ASIFormDataRequest requestWithURL:myLikesData_Url];
    [_requestMylikeInfoData setRequestMethod:@"POST"];
//    _requestMylikeInfoData.delegate = self;
    //用户名
    [_requestMylikeInfoData setPostValue:[NowDate getMD5string] forKey:@"ver_code"];
    [_requestMylikeInfoData setPostValue:[userDefault objectForKey:UserId] forKey:@"userID"];
//    NSLog(@"userID%@",[userDefault objectForKey:UserId]);
    //设备ID
    NSString *uid = [LTCustomerFactory getUUID];
    [_requestMylikeInfoData setPostValue:uid forKey:@"deviceID"];
    //传递数据页数
    NSNumber *pageNums = [NSNumber numberWithInt:oldInfoPage];
    [_requestMylikeInfoData setPostValue:pageNums forKey:@"page"];
    [_requestMylikeInfoData startSynchronous];
    
    //服务器返回信息
//    NSMutableDictionary   *responseDic=[[NSMutableDictionary alloc] init];
    
    if (_requestMylikeInfoData.responseData==NULL) {
        NSLog(@"_requestMylikeInfoData%@",_requestMylikeInfoData.responseData);
        return nil;
    }
    
    NSMutableDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:_requestMylikeInfoData.responseData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"mylike:%@",responseDic);
    if (responseDic) {
        NSArray *array = [responseDic objectForKey:@"myLikesData"];
        NSMutableArray *newArray = [[NSMutableArray alloc] init];
        if (array != nil && array.count>0) {
            //解析json
            for (int myI=0; myI<array.count; myI++) {
                NSDictionary *dataD = [array objectAtIndex:myI];
                if (dataD) {
                    ImageInfo *myLikeModel = [[ImageInfo alloc] initMyLikesWithDictionary:dataD];
                    [newArray addObject:myLikeModel];
                }
            }
            
        }
        oldInfoPage++;
        return newArray;
    }
    
    return nil;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值