使用AFNetworking无法从服务器获取数据。
这是我的一些代码,
NSString *serviceUrl = [NSString stringWithFormat:@"%@%@", BASE_URL,serviceName];
NSString *paramString = [NSString stringWithFormat:SERVICE_PARAMS, parametersString, DB_NAME];
NSData* data = [paramString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *parametersDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *postLength = [NSString stringWithFormat:@"%ld", [data length]];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[manager.requestSerializer setTimeoutInterval:SERVICE_TIMEOUT];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setValue:postLength forHTTPHeaderField:@"Content-Length"];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager POST:serviceUrl parameters:parametersDictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (self.delegate) {
[self.delegate onServiceSuccess:(NSDictionary *)responseObject];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (self.delegate) {
[self.delegate onServiceFailed];
}
}];
我得到这样的回应,< 5b5b7b22 636f756e 74223a22 30227d5d 5d> 但我的实际反应在邮递员工作正常。 这是邮递员屏幕
答案 0 :(得分:0)
您正在使用apt-get install <packages of you wants>
(默认响应序列化程序),因此AFHTTPResponseSerializer
是responseObject
个对象(NSData
是{{1}的基本<5b5b7b22 636f756e 74223a22 30227d5d 5d>
对象)。
您可以将description
替换为JSON对应的NSData
,或者自己进行序列化:
AFHTTPResponseSerializer
请注意,您的JSON是一个字典数组数组。所以这样做:AFJSONResponseSerializer
只是一个演员,一个坏演员。
如果您真的只想NSArray *myJSONArray = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
,请改为[self.delegate onServiceSuccess:(NSDictionary *)responseObject];
。