• IOS AFNetWorking 通过body传递参数给服务器


    https://www.jianshu.com/p/d3d31e55fafa

    https://blog.csdn.net/ximiaoweilai/article/details/102942632

    post请求体封装如下:

    - (void)postWithUrl:(NSString *)url body:(NSData *)body  success:(void(^)(NSDictionary *response))success failure:(void(^)(NSError *error))failur

    {

    NSString *requestUrl = @“”;

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    //如果你不需要将通过body传 那就参数放入parameters里面

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:nil];

    NSLog(@"requestURL:%@",requestUrl);

    request.timeoutInterval= 10;

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    // 设置body 在这里将参数放入到body

    [request setHTTPBody:body];

    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];

    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",

    @"text/html",

    @"text/json",

    @"text/javascript",

    @"text/plain",

    nil];

    manager.responseSerializer = responseSerializer;

    [[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response,id responseObject,NSError *error){

    if(responseObject!=nil){

    success(responseObject);

    }

    if (error) {

    failure(error);

    }

    }]resume];

    }

    调用:

    NSDictionary *dict = @{@"name":@"hello",@"sex":@"男"};

    NSData *data =    [NSJSONSerialization dataWithJSONObject:dict options:NSUTF8StringEncoding error:nil];

    [self postWithUrl:@"" body:data showLoading:0 success:^(NSDictionary *response) {

    //NSString *result = [[NSString alloc] initWithData:response  encoding:NSUTF8StringEncoding];

    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:nil];  //解析

    NSLog(@"%@",result);

    } failure:^(NSError *error) {

    }];

     

    IOS AFN 通过body传递参数给服务器

    2017-07-10 20:19 行走在砂砾中 

    1. 此段代码适用于请求接口时传参为json格式,而非常见的dictionary。同时设置请求头的Content-Type: application/json;charset=UTF-8
    2. 先将AFN文件导入 或者pod 。

    AFNetwoking的默认Content-Type是application/x-www-form-urlencodem。若服务器要求Content-Type为applicaiton/json,为了和服务器对应,就必须修改AFNetworking的Content-Type

    ===========刚在其他博主里看到更省事的代码,下面的这段代码不推荐。可以直接略过用第二段代码块===

    1.  
      NSDictionary *parameters= @{@"client_type":@"你的参数",@"你的参数":@"1"};
    2.  
       
    3.  
      //设置参数 根据你们服务器的格式设置。我们的后台需要传的是json格式的
    4.  
       
    5.  
      NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters 
    6.  
      options:NSJSONWritingPrettyPrinte  error:nil];
    7.  
       
    8.  
       //afn请求
    9.  
       AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:
    10.  
      [NSURLSessionConfiguration defaultSessionConfiguration]];
    11.  
       
    12.  
            NSString * requestUrl  = @"你的请求地址";
    13.  
          //如果你不需要在请求体里传参 那就参数放入parameters里面
    14.  
            NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]
    15.  
      requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:nil];
    16.  
       
    17.  
      // NSLog(@"requestURL:%@",requestUrl);
    18.  
      request.timeoutInterval= 10;
    19.  
       
    20.  
      //这句话很重要,设置"Content-Type"类型 json类型跟后台大哥的一致
    21.  
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    22.  
          // 设置参数放入到body请求体里。后台大哥让参数放在请求体里,因为没写这句代码,我TM调试浪费了半天
    23.  
          [request setHTTPBody:jsonData];
    24.  
       
    25.  
          AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    26.  
          responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
    27.  
      @"text/html","text/json", @"text/javascript",@"text/plain",  nil];
    28.  
       
    29.  
              manager.responseSerializer = responseSerializer;
    30.  
       
    31.  
          [[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil
    32.  
      completionHandler:^(NSURLResponse *response,id responseObject,NSError *error){
    33.  
       
    34.  
                 if(responseObject!=nil){
    35.  
       
    36.  
                     NSString *result = [[NSString alloc] initWithData:responseObject 
    37.  
      encoding:NSUTF8StringEncoding];
    38.  
        NSLog(@"%@",result);
    39.  
       
    40.  
                 }
    41.  
       
    42.  
             }]resume];

        第二种解决方法:推荐

    1.  
       
    2.  
      NSDictionary *parameters= @{@"client_type":@"你的参数",@"你的参数":@"1"};
    3.  
       
    4.  
       
    5.  
       
    6.  
      AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    7.  
      session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", nil];
    8.  
      //post 发送json格式数据的时候加上这两句。
    9.  
      session.requestSerializer = [AFJSONRequestSerializer serializer];
    10.  
      session.responseSerializer = [AFJSONResponseSerializer serializer];
    11.  
      session.requestSerializer.timeoutInterval = 15;
    12.  
      // NSString * requestUrl = @"http://47.244.143.48:8080/auth/getVerificationCode";
    13.  
      NSString * requestUrl = @"http://192.168.1.10:8080/auth/getVerificationCode";
    14.  
      [session POST:requestUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    15.  
       
    16.  
      } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    17.  
      if (responseObject) {
    18.  
      NSLog(@"post成功了%@",responseObject);
    19.  
      }
    20.  
       
    21.  
      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    22.  
      if (error) {
    23.  
      NSLog(@"post失败了%@",error);
    24.  
      }
    25.  
      }];
  • 相关阅读:
    innerHTML和innerText
    Function 构造器及其对象、方法
    构造函数
    jquery 获取及设置input各种类型的值
    在浏览器中输入URL并回车后都发生了什么?
    :after和:before的作用及使用方法
    使用JS监听键盘按下事件(keydown event)
    Javascript模块化编程(三):require.js的用法
    Javascript模块化编程(二):AMD规范
    Javascript模块化编程(一):模块的写法
  • 原文地址:https://www.cnblogs.com/itlover2013/p/13813900.html
Copyright © 2020-2023  润新知