我想在api中删除一些项目。我正在使用AFNetworking'〜> 2.5.4'版本。但是当我调用delete方法时,它会出现以下错误。
ERROR:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: client error (422)" UserInfo=0x7fc113d811c0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fc113d158b0> { URL: https://p1pptmol1j.execute-api.us-east-1.amazonaws.com/dev/v1/clients/favorites?item=Asian%20Chicken%20Salad } { status code: 422, headers {
"Access-Control-Allow-Headers" = "Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-access-token";
"Access-Control-Allow-Methods" = "POST,GET,DELETE,PUT,OPTIONS";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 118;
"Content-Type" = "application/json";
Date = "Thu, 28 Jul 2016 17:59:44 GMT";
Via = "1.1 e8211ae0170c9ccd38bcd06c168293e9.cloudfront.net (CloudFront)";
"X-Amz-Cf-Id" = "o6jvVIjhZV6-a4249Mh3QCUkECPt_wldNIODnFkVOKEe-87OQZmGLA==";
"X-Cache" = "Error from cloudfront";
"x-amzn-RequestId" = "109bb1a0-54ed-11e6-a157-f15ab424c3ca";
} }, NSErrorFailingURLKey=https://p1pptmol1j.execute-api.us-east-1.amazonaws.com/dev/v1/clients/favorites?item=Asian%20Chicken%20Salad, com.alamofire.serialization.response.error.data=<7b202263 6f646522 3a343232 30302c22 6d657373 61676522 3a22696e 76616c69 6420696e 70757422 2c226465 7461696c 73223a5b 22496e76 616c6964 20697465 6d2e206d 6179206e 6f742062 65206e75 6c6c222c 22496e76 616c6964 20697465 6d2e206d 6179206e 6f742062 6520656d 70747922 5d7d>, NSLocalizedDescription=Request failed: client error (422)}.
这是我的删除方法:(使用AFNetworking)
// DELETE API with Access Token using Arraylist WITH ACCESSTOKEN
+(void)DeleteDataFromApiWithAccessToken:(NSString *)url withParameters:(NSDictionary *)parameters accessTokenValue:(NSString *)tokenValue returnBlock:(arrayList)block
{
NSLog(@"loadDataFromApi withParameters :: url %@, params %@",url,parameters);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[serializer setValue:FF_API_HEADER_VALUE forHTTPHeaderField:FF_API_HEADER_FIELD];
[serializer setValue:tokenValue forHTTPHeaderField:ACCESSTOKEN_HEADER_FIELD];
//serializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", nil];
manager.requestSerializer = serializer;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// Check the Network Connection
if(![CommonClassApp checkForNetwork]) {
[CommonClassApp customAlert:ERR_MSG_NETWORK_PROBLEM];
if (block) block(NULL);
}else {
[manager DELETE:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"responseObject-->%ld",(long)[responseObject statusCode]);
if (responseObject != NULL)
{
NSError *error = nil;
NSMutableArray *responseJsonData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
NSLog(@"responseJsonData - %@",responseJsonData);
if (block) block(responseJsonData);
}else{
[CommonClassApp customAlert:ERR_MSG_INVALID_RESPONSE];
if (block) block(NULL);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error -->%@",error);
NSLog(@"failure ::: %ld",(long)[operation.response statusCode]);
NSDictionary *responseJsonData = [NSJSONSerialization JSONObjectWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] options:NSJSONReadingAllowFragments error:&error];
if ([operation.response statusCode] == 401) {
if (responseJsonData.count>0) {
NSString *userError = [responseJsonData valueForKey:@"code"];
[CommonClassApp customAlert:[ERROR_AUTH_ERROR valueForKey:userError]];
}
}
else
{
NSString *userError = [responseJsonData valueForKey:@"message"];
NSLog(@"Error Message: %@",userError);
[CommonClassApp customAlert:userError];
}
if (block) block(NULL);
}];
}}
所以,PLZ帮我解决了这个问题。感谢。