web-dev-qa-db-ja.com

AFnetworkingでコンテンツタイプを設定する方法

AFNetworkingに問題があります。設定できませんcontent-typeからapplication/json。どのようにできるのか?私はたくさんの解決策を試しますが、誰も働いていません。ここにあなたが私のコードです:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  if (self.enableBasicAuth){
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[SOWMerchant merchant]     username] password:[[SOWMerchant merchant] password]];
  }
  [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  manager.responseSerializer = [AFJSONResponseSerializer serializer];
  [manager POST:[NSString stringWithFormat:@"%@", url] parameters:postData success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [self onSuccessResponse:responseObject request:operation];
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self onError:error request:operation];
  }];

私はこの応答を受け取ります

{ URL: https://sbengtest.spotonway.com:9099/v1.0/merchant/check }, response: <NSHTTPURLResponse:  0x10aa20530> { URL: https://sbengtest.spotonway.com:9099/v1.0/merchant/check } { status code: 400,     headers {
Connection = "keep-alive";
"Content-Length" = 192;
"Content-Type" = "text/html";
Date = "Sun, 20 Jul 2014 22:34:59 GMT";
Server = "nginx/1.2.1";
} }>

更新:

私はこれを追加しようとします

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

operationManagerInstance.requestSerializer = requestSerializer;

アプリがクラッシュする

更新2:動作します。この行を削除しました

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
19
crox

これは私のために働いた!

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
10
thatzprem

このコードは機能します

客観的c

manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];

Swift

manager.responseSerializer.acceptableContentTypes=NSSet (object: "text/html") as Set<NSObject>
6