【问题标题】:Sending HTTP Body as string using AFNetworking or NSURLConnection使用 AFNetworking 或 NSURLConnection 将 HTTP 正文作为字符串发送
【发布时间】:2015-10-11 13:03:58
【问题描述】:

我正在使用一个 POST 网络服务,它采用这样的参数

jsonObj = {
  "id" : 0,
  "platform" : 1,
  "method" : "Home"
}

那么如何在参数中发送它我正在使用AFNetworking来消费webservice。

我的代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager.requestSerializer setValue:ContentType forHTTPHeaderField:@"Content-Type"];


NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"id",[NSNumber numberWithInt:1],@"platform",API_HomeProductList,@"method", nil];
 NSDictionary *param =[NSDictionary dictionaryWithObjectsAndKeys:dic,@"jsonObj", nil];
[manager POST:APIMainURL parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSMutableDictionary *json = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]]; //
    NSLog(@"JSON: %@",json);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}];

当我通过 AFNetworking 将其转换为 Json 后看到我的参数时,它变为有效的 json

{
  "jsonObj" = {
    "id" : 0,
    "platform" : 1,
    "method" : "Home"
  }
}

但是我得到了一些错误,因为我的网络服务占用了

jsonObj = {
   "id" : 0,
   "platform" : 1,
   "method" : "Home"
 } 

作为参数

所以请建议我如何发送

jsonObj = {
   "id" : 0,
   "platform" : 1,
   "method" : "Home"
}

在参数中。

【问题讨论】:

标签: ios objective-c iphone json afnetworking


【解决方案1】:

我想你误会了什么。您的网络需要

jsonObj={
   "id" : 0,
   "platform" : 1,
   "method" : "Home"
 } 

这意味着你需要创建一个 json

{
   "id" : 0,
   "platform" : 1,
   "method" : "Home"
 } 

所以,你只需要删除 1 行代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager.requestSerializer setValue:ContentType forHTTPHeaderField:@"Content-Type"];


NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"id",[NSNumber numberWithInt:1],@"platform",API_HomeProductList,@"method", nil];
// NSDictionary *param =[NSDictionary dictionaryWithObjectsAndKeys:dic,@"jsonObj", nil]; /// remove this
[manager POST:APIMainURL parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSMutableDictionary *json = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]]; //
    NSLog(@"JSON: %@",json);
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}];

【讨论】:

  • 没有我的网络服务需要 jsonObj={ "id" : 0, "platform" : 1, "method" : "Home" } 如果我按照指示显示错误。我必须通过附加 jsonObj= 来传递内部 json。
猜你喜欢
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多