先要确定是哪个引起的不能解析。
/**单例一个AFHTTPSessionManager*/
+ (AFHTTPSessionManager *)defaultManager{
static AFHTTPSessionManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [AFHTTPSessionManager manager];
//设置接受解析的内容
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json",@"text/plain",@"text/javascript",@"application/json", nil];
//需要驾驶下面两行
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
});
return manager;
}
+(id)GET:(NSString *)path parameters:(NSDictionary *)params completionHandle:(void (^)(id _Nonnull, NSError * _Nullable))completed{
NSLog(@"Request Method:GET,Path:%@,params:%@",path,params);
return [[self defaultManager] GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",task.response);//打印头文件,确定编码方式
//关键代码
NSError * error;
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
NSString* strdata = [[NSString alloc]initWithData:responseObject encoding:enc];//在将NSString类型转为NSData
NSData * data = [strdata dataUsingEncoding:NSUTF8StringEncoding];//这样解决的乱码问题。
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error ];
NSLog(@"%@",json);
completed(json,nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"GET ERROR:%@",error.userInfo);
}];
}
网友评论