美文网首页
四.AFNetworking请求 get与post

四.AFNetworking请求 get与post

作者: 择一城终老_蜗牛 | 来源:发表于2016-11-11 18:07 被阅读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) {
                   
                }];
    

    相关文章

      网友评论

          本文标题:四.AFNetworking请求 get与post

          本文链接:https://www.haomeiwen.com/subject/uwhfpttx.html