YTKNetWork升级到2.0版本后变化很大,现在针对返回数据格式“content-type”和配置默认参数做一个记录
1.先说一下返回数据格式“content-type”,直接上代码
#import <Foundation/Foundation.h>
@interface BaseNetConfig : NSObject
@singleton(BaseNetConfig);
-(void)configGlobalAPI;
@end
#import "BaseNetConfig.h"
#import <YTKNetworkConfig.h>
#import <YTKNetworkAgent.h>
#import "BaseUrlFilter.h"
#define BASE_URL_1 @"" //正式环境
#define BASE_URL_2 @"" //内部测试服务器
#define BASE_URL_3 @"" //外部测试服务器
@implementation BaseNetConfig
@def_singleton(BaseNetConfig);
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
-(void)configGlobalAPI
{
YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
config.baseUrl=BASE_URL_1;
config.cdnUrl=@"";
BaseUrlFilter *filter = [BaseUrlFilter filterWithArguments:nil];
[config addUrlFilter:filter];
//服务端返回格式问题,后端返回的结果不是"application/json",afn 的 jsonResponseSerializer 是不认的。这里做临时处理
YTKNetworkAgent *agent = [YTKNetworkAgent sharedAgent];
[agent setValue:[NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json",@"text/html", nil]
forKeyPath:@"jsonResponseSerializer.acceptableContentTypes"];
}
@end
特别需要注意的是[agent setValue:[NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json",@"text/html", nil]
forKeyPath:@"jsonResponseSerializer.acceptableContentTypes"];中key值改为了“jsonResponseSerializer.acceptableContentTypes”
2.配置默认参数
这里不解释原来的用法了,下面也是直接上代码:
#import <Foundation/Foundation.h>
#import "YTKNetworkConfig.h"
#import "YTKBaseRequest.h"
@interface BaseUrlFilter : NSObject<YTKUrlFilterProtocol>
+ (BaseUrlFilter *)filterWithArguments:(NSDictionary *)arguments;
@end
#import "BaseUrlFilter.h"
#import "AFURLRequestSerialization.h"
@implementation BaseUrlFilter{
NSDictionary *_arguments;
}
+ (BaseUrlFilter *)filterWithArguments:(NSDictionary *)arguments {
if (arguments==nil) {
int time = (int)[[NSDate date] timeIntervalSince1970];//时间戳
NSString *sign = [[[NSString stringWithFormat:@"%@%d%@%@",KAppId,time,kAppToken,KAppSignKay] MD5Hash] lowercaseString];//sign值
return [[self alloc]initWithArguments:@{@"appID":KAppId,@"ts":[NSString stringWithFormat:@"%d",time],@"sign":sign}];
}
else{
return [[self alloc] initWithArguments:arguments];
}
}
- (id)initWithArguments:(NSDictionary *)arguments {
self = [super init];
if (self) {
_arguments = arguments;
}
return self;
}
- (NSString *)filterUrl:(NSString *)originUrl withRequest:(YTKBaseRequest *)request {
NSLog(@"_arguments:%@",request.requestArgument);
return [self urlStringWithOriginUrlString:originUrl appendParameters:_arguments];
}
- (NSString *)urlStringWithOriginUrlString:(NSString *)originUrlString appendParameters:(NSDictionary *)parameters {
NSString *filteredUrl = originUrlString;
NSString *paraUrlString = [self urlParametersStringFromParameters:parameters];
if (paraUrlString && paraUrlString.length > 0) {
if ([originUrlString rangeOfString:@"?"].location != NSNotFound) {
filteredUrl = [filteredUrl stringByAppendingString:paraUrlString];
} else {
filteredUrl = [filteredUrl stringByAppendingFormat:@"?%@", [paraUrlString substringFromIndex:1]];
}
return filteredUrl;
} else {
return originUrlString;
}
}
- (NSString *)urlParametersStringFromParameters:(NSDictionary *)parameters {
NSMutableString *urlParametersString = [[NSMutableString alloc] initWithString:@""];
if (parameters && parameters.count > 0) {
for (NSString *key in parameters) {
NSString *value = parameters[key];
value = [NSString stringWithFormat:@"%@",value];
value = [self urlEncode:value];
[urlParametersString appendFormat:@"&%@=%@", key, value];
}
}
return urlParametersString;
}
- (NSString*)urlEncode:(NSString*)str {
return AFPercentEscapedStringFromString(str);
}
@end
网友评论