[英]AFNetworking version 3 content-type error
I'm making an iphone app that will interact with a particular ASP.NET server. 我正在制作一个将与特定ASP.NET服务器交互的iPhone应用程序。 I've got the following code to register: 我有以下代码要注册:
- (void)post:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure {
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"path: %@, %@", path, parameters);
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"parse-application-id-removed" forHTTPHeaderField:@"X-Parse-Application-Id"];
[manager.requestSerializer setValue:@"parse-rest-api-key-removed" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.securityPolicy.allowInvalidCertificates = YES;
[manager POST:path parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
[myDelegate StopIndicator];
success(responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
[myDelegate StopIndicator];
failure(error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:error.localizedDescription delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}];
}
//Register
-(void)registerUser:(NSString *)usertype firstname:(NSString *)firstname middlename:(NSString *)middlename lastname:(NSString *)lastname registration:(NSString *)registration password:(NSString *)password confirmpass:(NSString *)confirmpass email:(NSString *)email mobile:(NSString *)mobile success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
NSDictionary *requestDict = @{@"UserTypeId" : usertype, @"FirstName" : firstname, @"MiddleName" : middlename,@"LastName" : lastname, @"RegNumber" : registration ,@"Password": password,@"ConfirmPassword" : confirmpass, @"EmailAddress" : email, @"MobileNumber" : mobile};
[self post:kUrlSignup parameters:requestDict success:^(id responseObject) {
NSLog(@"registerWithEmail: %@", responseObject);
responseObject=(NSMutableDictionary *)[NullValueChecker checkDictionaryForNullValue:[responseObject mutableCopy]];
if([self isStatusOK:responseObject])
{
success(responseObject);
} else
{
[myDelegate StopIndicator];
failure(nil);
}
} failure:^(NSError *error)
{
[myDelegate StopIndicator];
failure(error);
}];
}
But it's giving me the following error having to do with Content-Type: 但这给了我以下与Content-Type有关的错误:
Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7c14e5c0> { URL: http://192.168.0.129/IISService/Service.svc/Registration } { status code: 415, headers {
"Cache-Control" = private;
"Content-Length" = 0;
Date = "Mon, 01 Aug 2016 05:38:44 GMT";
Server = "Microsoft-IIS/8.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://192.168.0.129/IISService/Service.svc/Registration, NSLocalizedDescription=Request failed: unsupported media type (415), com.alamofire.serialization.response.error.data=<>}
I am not sure if this error is from server side or I am making some mistake. 我不确定这个错误是来自服务器端还是我犯了一些错误。 How can I fix this error ? 如何解决此错误?
By default, the AFHTTPResponseSerializer
support deal with response of request which Content-Type
equal either of application/json
, text/json
, text/javascript
. 默认情况下, AFHTTPResponseSerializer
支持处理Content-Type
等于application/json
, text/json
, text/javascript
之一的请求响应。 If your server return a response which Content-Type
equal text/html
or other, the AFHTTPResponseSerializer
will not deal with your response and throw a error. 如果您的服务器返回的Content-Type
等于text/html
或其他Content-Type
的响应,则AFHTTPResponseSerializer
将不会处理您的响应并抛出错误。 You can add accept types by youself as below: 您可以自己添加接受类型,如下所示:
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
NSMutableSet *acceptTypes = [NSMutableSet setWithSet:serializer.acceptableContentTypes];
[acceptTypes addObject:@"your-content-type"];
serializer.acceptableContentTypes = acceptTypes;
manager.responseSerializer = serializer;
AFHTTPSessionManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username":username, @"password":password};
[manager POST:@"URL Here" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
This does the creation of the request, setting its Content-Type according to the requestSerializer setting, and encodes the JSON for you. 这将创建请求,根据requestSerializer设置设置其Content-Type,并为您编码JSON。
Update 1:- 更新1:-
+(void)callWebServicePOST_withAPIName:(NSString *)url withParameters:(id)parameters withBlock:(ResponseCompilationBlock)_Block {
[[AFHTTPRequestSerializer serializer] setTimeoutInterval:60];
[[AFHTTPSessionManager manager].requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[[AFHTTPSessionManager manager] POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog (@"response:%@", responseObject);
_Block (YES, 200, responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error:%@", error);
_Block (NO, 400, error);
}];
}
Declare this method in AppDelegate file and call it just like this:- 在AppDelegate文件中声明此方法,然后像这样调用它:
[Appdelegate callWebServicePOST_withAPIName:@"url string" withParameters:params withBlock:^(BOOL isSuccess, NSInteger statusCode, id response) {
if (isSuccess) {
/* Do further procedure here */
}
else {
NSLog (@"%@", [error localizedDescription])
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.