这就是我发送POST请求的方式:
{if (![[NetworkManager sharedManager] connected]) {
[[NetworkManager sharedManager].requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NetworkManager sharedManager].responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"];
[[NetworkManager sharedManager] POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (responseObject != nil) {
NSLog(@"JSON === %@",responseObject);
successBlock(responseObject,task);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error != nil) {
failureBlock(error,task);
}
}];
}else
{
failureBlock(nil,nil);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication].keyWindow makeToast:@"Please check your internet connection"
duration:2.0
position:CSToastPositionBottom];
});
}}
但是我收到错误“请求失败:不可接受的内容类型:text / html”。我正在使用AFNetworking
。请帮忙!!
答案 0 :(得分:0)
@Ghazalah试试这个..
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:url parameters:dictParam progress:^(NSProgress * _Nonnull uploadProgress)
{
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
DLog(@"JSON: %@", responseObject);
if (dataBlock)
{
if(responseObject==nil)
dataBlock(task.response,nil);
else
dataBlock(responseObject,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
DLog(@"Error: %@", error);
if (dataBlock) {
dataBlock(nil,error);
}
}];
答案 1 :(得分:0)
当您的服务器发送@" text / html"而不是已经支持的类型(@" application / json")。我的解决方案是添加" text / html"到AFURLResponseSerialization类中设置的acceptableContentTypes。只需搜索" acceptableContentTypes"并添加@" text / html"到手动设置。
{
if (![[NetworkManager sharedManager] connected]) {
[[NetworkManager sharedManager].requestSerializer setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[NetworkManager sharedManager].responseSerializer.acceptableContentTypes = [[NetworkManager sharedManager].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[[NetworkManager sharedManager] POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (responseObject != nil) {
NSLog(@"JSON === %@",responseObject);
successBlock(responseObject,task);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error != nil) {
failureBlock(error,task);
}
}];
}
else{
failureBlock(nil,nil);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication].keyWindow makeToast:@"Please check your internet connection"
duration:2.0 position:CSToastPositionBottom];
});
}
}
理想的解决方案是更改从服务器发送的类型,但为此,您必须与服务器团队交谈。