Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

ios7 - Post Request with AFHttpRequestOperationManager not working

I am using AFHTTPRequestOperationManager to post some JSON to my server, my code is below.

NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"john", @"name", @"[email protected]", @"email", @"xxxx", @"password", @"1", @"type", nil];
// Do any additional setup after loading the view.
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];

[operationManager POST:@"posturl here" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", [responseObject description]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [error description]);
}];

and the response is as follows:

2013-11-18 16:49:29.780 SwapOnceApiTester[12651:60b] Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unsupported media type (415), got 1664256" UserInfo=0x1565a6c0 {NSErrorFailingURLKey=xxxxxxx, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x15656db0> { URL: xxxxxxxxx } { status code: 415, headers {
    "Cache-Control" = "max-age=604800";
    Connection = "keep-alive";
    "Content-Type" = "application/json";
    Date = "Mon, 18 Nov 2013 11:49:28 GMT";
    Expires = "Mon, 25 Nov 2013 11:49:28 GMT";
    Server = nginx;
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = PleskLin;
} }, NSLocalizedDescription=Request failed: unsupported media type (415), got 1664256}

I dont know what the problem is with this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to set your request and response serializers to handle JSON using AFJSONRequestSerializer and AFJSONResponseSerializer before executing your request. Using an NSDictionary literal for your parameters helps code clarity as well:

AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];

[operationManager POST:@"posturl here" 
            parameters: @{@"name":  @"john",
                        @"email":   @"[email protected]",
                        @"password: @"xxxxx",
                        @"type":    @"1"}
               success:^(AFHTTPRequestOperation *operation, id responseObject) {
                   NSLog(@"JSON: %@", [responseObject description]);
               } 
               failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                   NSLog(@"Error: %@", [error description]);
               }
];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...