iOS 学习日志 : 向服务器发送Json数据

前几天接口需要我向服务器发送一个Json数据,但是利用AFNetWorking中的AFHTTPSessionManager发送到服务器,接口接收的串一直是URL编码过后的串,无法和安卓统一,于是换了另一种方法,利用AFHTTPSessionManager的子类AFHTTPRequestOperation设置UTF-8编码才成功,用AFHTTPSessionManager设置UTF-8编码是无效的,不知道其中哪个环节出了问题,断电追到里面,发现设置无效。

NSDictionary * dict = @{@"id":@"12312312",@"name":@"test"};
            NSData * data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
            NSString * json = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            //字典转Json串 为了方便,避免自己拼串出错]]
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
            [request setHTTPMethod:@"POST"];
            [request setHTTPBody:[json dataUsingEncoding:NSUTF8StringEncoding]];

            [request setValue:@"text/html;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            //]]
            operation.responseSerializer = [AFJSONResponseSerializer serializer];
            //]]
            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                //成功]]
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                //失败
            }];
            [operation start];

你可能感兴趣的:(iOS)