如何使用AFNetworking传递数组值?

时间:2016-04-07 11:24:55

标签: ios objective-c paypal afnetworking

现在我正在使用API​​S进行paypal重复集成,我面临以下API中的请求格式问题。 如何发送请求格式与其他人一样。

curl -v -k -X PATCH 'https://api.sandbox.paypal.com/v1/payments/billing-plans/P-94458432VR012762KRWBZEUA' \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer <Access-Token>" \
    -d '[
        {
            "path": "/",
            "value": {
                "state": "ACTIVE"
            },
            "op": "replace"
        }
    ]' 

到目前为止,我已尝试过以下代码

 NSDictionary *valueDic = @{@"state":@"ACTIVE"};
    NSDictionary *parametersDic = @{@"path":@"/",@"value":valueDic,@"op":@"replace"};
    NSMutableArray *parameterArray = [[NSMutableArray alloc] init];
    [parameterArray addObject:parametersDic];

    NSString *finalyToken = [[NSString alloc]initWithFormat:@"Bearer %@",strAccessToken];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:finalyToken forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json+patch" forHTTPHeaderField:@"Content-Type"];
    [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json"];

    NSString *urlPatch = [NSString stringWithFormat:@"https://api.sandbox.paypal.com/v1/payments/billing-plans/%@",creditCardId];
    [manager PATCH:urlPatch parameters:parameterArray success:^(AFHTTPRequestOperation *operation, id responseObject){
         NSLog(@"JSON: %@", responseObject);
        [self recurringCreateBillingAgreement];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       NSLog(@"Error: %@", error);

我已经创建了如上所述的请求格式

(
{
    op = replace;
    path = "/";
    value =     {
        state = ACTIVE;
    };
}
)

但是,我收到了以下错误

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo=0x177155d0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x1755c9b0> { URL: https://api.sandbox.paypal.com/v1/payments/billing-plans/P-1DT55351E261652436BZHSLA } { status code: 500, headers {
    "CORRELATION-ID" = 6bf7cbf78bb16;
    Connection = "close, close";
    "Content-Length" = 196;
    "Content-Type" = "text/xml;charset=UTF-8";
    Date = "Thu, 07 Apr 2016 11:19:08 GMT";
    "PROXY_SERVER_INFO" = "host=slcsbplatformapiserv3001.slc.paypal.com;threadId=368";
    "Paypal-Debug-Id" = "6bf7cbf78bb16, 6bf7cbf78bb16";
    Server = Apache;
    "Set-Cookie" = "X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Dplatformapiserv%26TIME%3D742524503; domain=.paypal.com; path=/; Secure; HttpOnly, X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT";
    Vary = Authorization;
} }, NSErrorFailingURLKey=https://api.sandbox.paypal.com/v1/payments/billing-plans/P-1DT55351E261652436BZHSLA, NSLocalizedDescription=Request failed: internal server error (500), com.alamofire.serialization.response.error.data=<3c6e7331 3a584d4c 4661756c 7420786d 6c6e733a 6e73313d 22687474 703a2f2f 6378662e 61706163 68652e6f 72672f62 696e6469 6e67732f 78666f72 6d617422 3e3c6e73 313a6661 756c7473 7472696e 6720786d 6c6e733a 6e73313d 22687474 703a2f2f 6378662e 61706163 68652e6f 72672f62 696e6469 6e67732f 78666f72 6d617422 3e6a6176 612e6c61 6e672e4e 756c6c50 6f696e74 65724578 63657074 696f6e3c 2f6e7331 3a666175 6c747374 72696e67 3e3c2f6e 73313a58 4d4c4661 756c743e>, NSUnderlyingError=0x1757d4c0 "Request failed: unacceptable content-type: text/xml"}

1 个答案:

答案 0 :(得分:0)

您需要将JSON字符串作为一个参数发送。 您可以创建数组的JSON字符串,如下所示。

NSData *data = [NSJSONSerialization dataWithJSONObject:parameterArray options:0 error:nil];
NSString* jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

parameterArray将是您的数组

现在使用jsonString作为您的某个参数的值,以便将其发送到服务器。