簡體   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