ASI网络请求,同步、异步

来源:互联网 发布:李天生sql server 2008 编辑:程序博客网 时间:2024/05/27 00:50

异步请求:

第一步:实现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;}


0 0
原创粉丝点击