美文网首页
iOS AFNetworking用JSON类型数据发送POST请

iOS AFNetworking用JSON类型数据发送POST请

作者: 程序猿类码农 | 来源:发表于2020-03-14 15:17 被阅读0次

    以前给后端传参数都是post form(NSDictionary)方式,今天后端要求我用post json方式,开始直接懵逼状态,这也没用过啊,找了好多的资料,最后在AFN里面找到了AFJSONRequestSerializer,代码如下

        NSDictionary*dict =@{

            @"value":@(self.tempareValues),

            @"timepoint":self.currentTime,

            @"deviceCode":m_deviceCode,

            @"iValue":@(self.ivalueNum)

        };   

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

            NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:self.saveJsonArray error:nil];

            request.timeoutInterval=10.f;

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

            [requestsetValue:@"application/json" forHTTPHeaderField:@"Accept"];

            [requestsetValue:YYUserToken forHTTPHeaderField:@"Authorization"];

            dispatch_time_tdelayTime =dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/* NSEC_PER_SEC));

            NSURLSessionDataTask*task = [managerdataTaskWithRequest:requestcompletionHandler:^(NSURLResponse*_Nonnullresponse,id  _NullableresponseObject,NSError*_Nullableerror) {

                if(!error) {

                

                }else{

                    NSLog(@"请求失败error=%@", error);

                }

            }];

            [taskresume];

    相关文章

      网友评论

          本文标题:iOS AFNetworking用JSON类型数据发送POST请

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