简体   繁体   中英

Sending HTTP Body as string using AFNetworking or NSURLConnection

I am consuming a POST web service which take the Parameter like this

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

So how to send it in parameter I am using AFNetworking to consume webservice.

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

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

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

So, you just need remove 1 line code

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;

}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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