用AFNetWorking框架中的AFJSONRequestOperation下载json数据是一直出错。
打断点跟踪后发现,每次都没有走到success语法块里面,而是直接走到failure语法块里面。
我想把failure语法块里面的错误信息打印出来
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"failure:%@",[error description]);
}
输出的结果是这样的
failure:Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html" UserInfo=0x84849d0 {NSLocalizedRecoverySuggestion={"message":["用户名为空!"],"data":[null],"success":false}, AFNetworkingOperationFailingURLRequestErrorKey=<NSURLRequest http://192.168.0.99:8080/jlsj/ios_mobileLogin.action?mobileUserName=A&mobilePassword=A>, NSErrorFailingURLKey=http://192.168.0.99:8080/jlsj/ios_mobileLogin.action?mobileUserName=A&mobilePassword=A, NSLocalizedDescription=Expected content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8443230>}
原来是缺少它需要的字符集,于是在前面加上
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/html", nil]];
这下终于可以成功运行了。