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
- ASI网络请求,同步、异步
- 同步/异步网络请求
- 网络请求同步异步
- 网络:ASI 框架使用(同步与异步)
- 网络请求分为同步异步
- ios同步、异步请求网络
- ASI基本使用-异步请求
- ASI基本使用-同步请求
- iOS-网络-同步请求、异步请求、GET请求、POST请求
- 网络请求 同步get 同步post 异步get 异步post
- ASI使用介绍(同步与异步)
- IOS_网络请求_get+post+同步+异步
- 网络的同步和异步请求
- 网络同步异步请求的图片讲解
- iOS网络基础--发送同步、异步请求
- ios同步与异步网络请求
- 42.网络请求POST同步和异步
- iOS网络请求GET&POST,同步&异步
- char *s 和 char s[] 的区别小结
- 成绩转换 NYOJ 98
- 最小生成树——畅通工程再续
- 【Discuz】discuz如何设置VIP特殊用户组免回复可看隐藏内容帖X1.5----X3.2等版本均适合
- iOS 工厂模式
- ASI网络请求,同步、异步
- Cloudera Impala官方文档中文翻译-1
- 一个GUI程序究竟会有几个线程?
- HDU 2046 骨牌铺方格
- android 删除AVD模拟器上的应用(APP)
- hdu 2952 Counting Sheep
- LeetCode-Restore IP Addresses<ERROR>
- LoadRunner压测需要关注的Windows Resources
- Android Handler 机制