-(void) loadDataFromServer
{
NSString *strurl =[NSStringstringWithFormat:@"%@/menus.php",HTTP_SERVER];
NSURL *url = [NSURL URLWithString:strurl];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
//getMenusList 请求数据
[postDic setObject:@"getMenusList" forKey:@"action"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDicoptions:NSJSONWritingPrettyPrinted error:nil]; //data是字符串
NSString *postString = [[NSString alloc] initWithData:postDataencoding:NSUTF8StringEncoding];//把data格式的postdata转换成string格式
[request setPostValue:postString forKey:@"data"]; //传值
[request setCompletionBlock:^{
//阻塞块可以吧整个函数作为变量,传到setCompletionBlock中
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:request.responseDataoptions:NSJSONReadingMutableContainers error:nil];
//把服务器返回的json字符串的data转化成dictionary
if([[dic objectForKey:@"statusID"] isEqualToString:@"0"])
{
NSArray *arr = [dic objectForKey :@"reArray"];
NSDictionary *dic = [[NSDictionary alloc]init];
for(dic in arr)
{
NSString *fasttypeStr = [dic objectForKey:@"MType"];
if([fasttypeStr isEqualToString:@"快餐"])
{
[self.tableArray addObject:dic];
//[self.tableArray addObject:[arr objectAtIndex:i]];
[self startDownLoadImageFromServer];
//[_tableView reloadData];
}
//
}
}
else
{
NSString *msg = [dic objectForKey:@"msg"];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示"message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertV show];
[alertV release];
}
[_tableView reloadData];
NSLog(@"%@",request.responseString);
}]; //返回数据
[request setFailedBlock:^{
}];
[request startAsynchronous];
[postString release];
}
//开始下载图片
-(void) startDownLoadImageFromServer
{
for (NSDictionary *dic in self.tableArray)
{
NSString *imgurl = [dic objectForKey:@"MImage"];
NSString *imageUrlStr = [NSStringstringWithFormat:@"%@/%@",HTTP_SERVER,imgurl];//HTTP_SERVER是定义的宏,imageUrlStr是服务器上图片的地址
NSURL *url = [NSURL URLWithString:imageUrlStr];
ASIHTTPRequest *requst = [ASIHTTPRequest requestWithURL:url];
[requst setCompletionBlock:^{
UIImage *imageData = [UIImage imageWithData:requst.responseData];
if (!imageData)
{
return;
}
NSMutableDictionary *dic2 = [NSMutableDictionarydictionaryWithDictionary:dic];
[dic2 setObject:imageData forKey:@"image2"];
int inx = [self.tableArray indexOfObject:dic];
if(inx != NSNotFound) //如果找到inx(因为刷新后tablearray里的地址变了,inx就为-1,说明没找到)
{
[self.tableArray replaceObjectAtIndex:inx withObject:dic2];
//user.userImage = [UIImage imageWithData:requst.responseData];//responseData就是图片数据
[_tableView reloadData];
}
}];//请求下载
[requst startAsynchronous];
}
}