我在失败块中获取json字符串
NSURL *url = [[NSURL alloc] initWithString:@"http://www.vinipost.com/Services/Update/UpdateService.asmx/GetPropSubType?"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
输出:
Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
"text/json",
"text/javascript",
"application/json",
"text/html"
)}, got text/plain" UserInfo=0x71521a0 {NSLocalizedRecoverySuggestion=[{"PropTypId":1,"PropCatId":1,"PropTyp":"Flat/ Condo"}.......**
答案 0 :(得分:10)
您需要在操作前添加此行
[AFJSONRequestOperation addAcceptableContentTypes:
[NSSet setWithObject:@"text/plain"]];
答案 1 :(得分:5)
错误很明显: Web服务返回错误的内容类型。 内容类型应为以下之一:
“文本/ JSON”, “文/ JavaScript的”, “应用程序/ JSON”, “text / html的”
但它返回
文本/纯
此外,如果您查看http响应,它会在其中返回HTML TAGS,因此AFNetworking无法解析。
如果这个页面:
http://www.vinipost.com/Services/Update/UpdateService.asmx/GetPropSubType?
在您的控制之下,纠正删除html标记和更改内容类型的行为
答案 2 :(得分:4)
在AFNetworking中,您必须在AFHTTPClient的帮助下创建NSURLRequest(首先您必须创建AFHTTPClient并且必须为此对象设置一些属性),如下所示
AFHTTPClient *httpClient = [[httpClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.vinipost.com/"]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
httpClient.parameterEncoding = AFJSONParameterEncoding;
现在如果依赖于GET / POST或任何其他类型的请求你需要设置参数我认为它是POST请求,所以设置参数dict并正确设置所有必需的键值对。如果不需要参数,你可以传递参数as nil
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"Services/Update/UpdateService.asmx/GetPropSubType?" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSLog(@"%@",JSON);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"Error MSG = %@",error);
}];
[operation start];
希望这对您有用:)