繁体   English   中英

使用AFNetworking或NSURLConnection将HTTP正文作为字符串发送

[英]Sending HTTP Body as string using AFNetworking or NSURLConnection

我正在使用一个带有这样参数的POST Web服务

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

那么如何在参数中发送它我正在使用AFNetworking来消耗Web服务。

我的密码

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"
}

在参数中。

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

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;

}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM