四.AFNetworking请求 get与post
2016-11-11 本文已影响0人
择一城终老_蜗牛
对服务器返回数据的解析
1.AFN可以自动对服务器返回的数据进行解析* 默认将服务器返回的数据当做JSON来解析
2.设置对服务器返回数据的解析方式
1> 当做是JSON来解析(默认做法)
*mgr.responseSerializer = [AFJSONResponseSerializer serializer];
*responseObject
的类型是NSDictionary
或者NSArray
2> 当做是xml来解析
*mgr.responseSerializer = [AFXMLParserResponseSerializer serializer];
*responseObject
的类型是NSXMLParser
3> 直接返回data* 意思是:告诉AFN不要去解析服务器返回的数据,保持原来的data即可
*mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
AFNetworking请求 get与post
1. AFHTTPRequestOperationManager * mager=[AFHTTPRequestOperationManager manager];
mager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html”];
//参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
params[@"uid"]=[userDefaultes stringForKey:@"uid"];
params[@"page"]=[NSString stringWithFormat:@"%d",_page];
params[@"kw"]=searchString;
//get方法
[mager GET:[NSString stringWithFormat:@"%@/DoctorIndex2/searchHzDoctor",API] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableArray *dataArray=responseObject[@"data"];
int code=[responseObject[@"code"]intValue];
if(code==1)
{
for(int i=0;i<dataArray.count;i++)
{
NSDictionary *dic=dataArray[i];
DoctorModel *model=[DoctorModel new];
model.docterID=[NSString stringWithFormat:@"%d",[dic[@"id"]intValue]];
model.avatar=dic[@"avatar"];
[_searchArray addObject:model];
}
}else//当code是0或者-1
{
}
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
//post方法
AFHTTPRequestOperationManager * mager=[AFHTTPRequestOperationManager manager];
mager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"openid"]=snsAccount.usid;
params[@"sdk_type"]=@"0";
[mager GET:[NSString stringWithFormat:@"%@/UserAuths/query",API] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"微信提交openid返回的数据:%@",responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];