zoukankan      html  css  js  c++  java
  • iOS AFN向接口端传递JSON数据

    NSDictionary *body = @{@"snippet": @{@"topLevelComment":@{@"snippet":@{@"textOriginal":self.commentToPost.text}},@"videoId":self.videoIdPostingOn}};
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&access_token=%@",[[LoginSingleton sharedInstance] getaccesstoken]] parameters:nil error:nil];
    
    req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    
        if (!error) {
            NSLog(@"Reply JSON: %@", responseObject);
    
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                  //blah blah
            }
        } else {
            NSLog(@"Error: %@, %@, %@", error, response, responseObject);
        }
    }] resume];
  • 相关阅读:
    Func<>委托、扩展方法、yield、linq ForEach综合运用
    EntityFramework学习要点记一
    MVC模型验证
    MVC过滤器
    解决EntityFramework与System.ComponentModel.DataAnnotations命名冲突
    Linq操作之Except,Distinct,Left Join 【转】
    WebClient小结
    Code First项目Migrations
    jquery之on()绑定事件和off()解除绑定事件
    Bootstrap页面响应式设计
  • 原文地址:https://www.cnblogs.com/wlsxmhz/p/5898129.html
Copyright © 2011-2022 走看看