报错信息如下:
error:Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7faddb024460 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7faddb0a4ed0> { URL: http://xhyj.bloveambition.com/index.php?r=patriarch/user/feedback } { status code: 200, headers {
"Centent-Type" = "text/html;charset=UTF-8";
Connection = "keep-alive";
"Content-Type" = "text/html";
Date = "Tue, 10 Feb 2015 08:14:17 GMT";
Pragma = "no-cache";
Server = "nginx/1.0.15";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "PHP/5.3.3";
} }, NSErrorFailingURLKey=http://xhyj.bloveambition.com/index.php?r=patriarch/user/feedback, com.alamofire.serialization.response.error.data=<7b227374 61746522 3a317d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
解决办法:
2.0已经对各种方法做了优化,也不需要用AFJSONRequestOperation,2.0已经自带JSON解析。
你可以用AFHTTPRequestOperationManager解决一切问题,2.0会对返回的JSON或者XML自动解析为字典的
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//设置相应内容类型
上面的报错内容类型是 "text/html":
/**
* 5.19 意见反馈Post
*
* @param id 用户id
* @param token 用户token
* @param content 反馈的文本信息
* @param successBlock
*/
+ (void)userFeedbackWithcontent:(NSString *)content
successBlock:(void(^)(BOOL isSuccess,NSDictionary *resultDic))successBlock
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//设置相应内容类型
NSDictionary *paramters = @{@"id": [self getUserId],@"token":[self getUserToken],@"content":content};
NSLog(@"5.19 意见反馈Post paramters:%@",paramters);
[manager POST:[NSString stringWithFormat:@"%@index.php?r=patriarch/user/feedback",kMainUrlString] parameters:paramters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"5.19 意见反馈Post:%@",responseObject);
if ([responseObject[@"state"] intValue] == 1) {//请求成功,状态不一样
successBlock(YES,responseObject);
}else {
successBlock(NO,responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
successBlock (NO,nil);
NSLog(@"5.19 意见反馈POST请求error:%@",error);
}];
}