在使用AFNetworking的时候可能会碰到下面的错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x1782695c0 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x170031d00> { URL: http://m.weather.com.cn/data/101010100.html } { status code: 200, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html; charset=utf-8";
Date = "Sat, 10 May 2014 08:36:57 GMT";
P3P = "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"";
Server = "nginx/0.7.67";
"Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=http://m.weather.com.cn/data/101010100.html}
此时需要修改AFNetworking可接收的Content-Type,前往AFNetworking源代码目录找到AFURLResponseSerialization.m文件将里面的代码:self.acceptableContentTypes = [NSSetsetWithObjects:@"application/json",@"text/json",@"text/javascript",nil];
修改为:
self.acceptableContentTypes = [NSSetsetWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
下面我们使用国家气象局提供免费的天气预报接口做个测试,注意在访问时候需要指定城市代码。
例如:http://m.weather.com.cn/data/101010100.html,其中的101010100指的就是城市北京。