我试图通过使用GeoNames API获得响应。在这里我的代码
NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];
NSURL *baseURL = [NSURL URLWithString:@"http://api.geonames.org/findNearbyPostalCodesJSON"];
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
我收到了以下错误。
Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (403)" UserInfo={NSUnderlyingError=0x7ff013d840f0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff013d07440> { URL: http://api.geonames.org/findNearbyPostalCodesJSON/ }
我尝试用hurl.it
检查是否有响应。它即将到来。
惊喜是我使用相同的上述代码用于各种其他请求,只更改URL并且这些正常工作。
更新
之前它适用于以下代码。我做了一些代码质量调整,并将代码转移到上面。然后得到了问题
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"lat": lat,
@"lng": lon,
@"username" : @"testing"
};
[manager POST:@"http://api.geonames.org/findNearbyPostalCodesJSON" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
}
答案 0 :(得分:1)
当我在浏览器中输入URL http://api.geonames.org/findNearbyPostalCodesJSON时,我会得到以下JSON:
{"status":{"message":"Please add a username to each call in order for geonames to be able to identify the calling application and count the credits usage.","value":10}}
所以我怀疑,至少,你需要将'POST'更改为'GET':
[manager GET:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
除此之外,基于返回的内容,您似乎需要一些额外的应用程序级逻辑来提供用户身份/身份验证,以便获取您感兴趣的实际数据,但上述更改应该在至少触发成功块的调用。
答案 1 :(得分:1)
我找到了解决方案。我甚至将AFNetworking从2.X升级到3.0
和代码更改如下。特殊变更GET
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager GET:@"http://api.geonames.org/findNearbyPostalCodesJSON" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success!");
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error: %@", error);
}];
答案 2 :(得分:0)
&#34;请求失败:不可接受的内容类型:text / html&#34;
因为AFNetworking只支持@&#34; application / json&#34;,@&#34; text / json&#34;,@&#34; text / javascript&#34;
你应该添加一个@&#34; text / html&#34;输入
[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];
NSURL *baseURL = [NSURL URLWithString:@"http://api.geonames.org/findNearbyPostalCodesJSON"];
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//insert this code
[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
[delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];