我正在使用AFNetworking 2.0从我正在构建的服务中读取JSON(现在在localhost
上)。很正常的东西。
Node正在发送JSON,如下所示:
res.setHeader('Content-Type','application/json');
res.end( JSON.stringify(...));
我的iOS第一遍代码试图像这样读取数据:
typedef void(^NextBlock)();
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:self.newestTimestampURL.absoluteString
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//NSDictionary *response =
NSLog(@"got %@", responseObject );
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"fail %@", error );
}];
这是我得到的错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0xb783e30 {NSErrorFailingURLKey=http://localhost:3000/api/v1/log/newest, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb656a70> { URL: http://localhost:3000/api/v1/log/newest } { status code: 200, headers {
Connection = "keep-alive";
ContentType = "application/json";
Date = "Fri, 27 Dec 2013 20:58:50 GMT";
"Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}
我可以卷曲(-i)url http://localhost:3000/api/v1/log/newest
并获取我期望的数据,并且它是application / json,如预期的那样。如果我在我的Web浏览器中加载该数据,我会按照预期的工具检查器获得JSON。
但是使用AFNetworking,我得到了这个神秘的“不可接受的内容类型:text / plain”错误。有什么想法吗?
注意:我之前从未使用过AFNetworking,所以我可能错误地使用它。
答案 0 :(得分:40)
服务器似乎正在发送&#34; text / html&#34;,默认情况下不支持此类型。添加@&#34; text / html&#34; for&#34; acceptableContentTypes&#34;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
答案 1 :(得分:21)
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
为我工作。
答案 2 :(得分:9)
为我工作。
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
答案 3 :(得分:4)
只需使用:
(SWIFT):
manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>
OBJ-C:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
答案 4 :(得分:4)
我已经按照@KIO发布的答案,但它在我的方案中没有用。
在得到许多答案之后,我能够找到适合我的解决方案。
AFHTTPRequestOperationManager *operation = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
AFJSONResponseSerializer *jsonResponseSerializer = [AFJSONResponseSerializer serializer];
NSMutableSet *jsonAcceptableContentTypes = [NSMutableSet setWithSet:jsonResponseSerializer.acceptableContentTypes];
[jsonAcceptableContentTypes addObject:@"text/plain"];
jsonResponseSerializer.acceptableContentTypes = jsonAcceptableContentTypes;
operation.responseSerializer = jsonResponseSerializer;
答案 5 :(得分:0)
NSURL *url = [NSURL URLWithString:RSS_WORLD_NEWS];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// Make sure to set the responseSerializer correctly
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/rss+xml"];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
[XMLParser setShouldProcessNamespaces:YES];
XMLParser.delegate = self;
[XMLParser parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving contents"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
为我工作
答案 6 :(得分:0)
更新了Swift解决方案。
如果您在Swift中使用AFNetworking。那么这个解决方案可能对你有帮它将接受大多数内容类型。
let manager=AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments) as AFJSONResponseSerializer
manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer
manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/html", "text/plain", "text/json", "text/javascript", "audio/wav") as Set<NSObject>