我第一次尝试在一个项目上使用 AFNetworking 并且无法将浮点数设置double
为 JSON 参数。我正在创建一个带有键值对的 NSDictionary。NSDictionary 仅接受对象,因此使用创建双精度值[NSNumber numberWithDouble: 37.234567]
。都好。但是当我转储字典时,我得到:
Dict: {
day = Wednesday;
user = Meltemi;
lat = "37.234567";
long = "-120.123456";
}
其中,当作为 HTTP 请求提交时,变为:lat=%2237.234567%22
并且long=%22-120.123456%22
Sinatra 将它们接收为:
params: { "lat"=>"\"37.750256\"", "long"=>"\"-120.123456\"", ... }
我想要/需要的:
Dict: {
day = Wednesday;
user = Meltemi;
lat = 37.234567;
long = -120.123456;
}
API 很可能不能容错,但如何强制 AFNetworking 丢失引号?
编辑:添加 AFHTTPClient 子类的初始化程序:
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if(!self) return nil;
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
[self setParameterEncoding:AFJSONParameterEncoding];
[self setAuthorizationHeaderWithUsername:@"user" password:@"pwd"];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; // TODO: right place for this?
return self;
}