简体   繁体   English

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

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

I am consuming a POST web service which take the Parameter like this 我正在使用一个带有这样参数的POST Web服务

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

So how to send it in parameter I am using AFNetworking to consume webservice. 那么如何在参数中发送它我正在使用AFNetworking来消耗Web服务。

My Code 我的密码

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;

}];

When I see my parameter after converting it to Json by AFNetworking it become that is valid json 当我通过AFNetworking将其转换为Json后看到我的参数时,它变为有效json

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

but And I get some error cause my web service take 但是我遇到一些错误,导致我的网络服务

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

as parameter 作为参数

So Please suggest me how to send 所以请建议我如何发送

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

in Parameter. 在参数中。

I think you misunderstood something. 我想你误会了。 Your web takes 您的网络需要

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

it means you need create a json 这意味着您需要创建一个json

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

So, you just need remove 1 line code 因此,您只需要删除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