XTNetworkEngine
HTTP请求底层能力处理类,封装了请求参数构造和响应数据的处理,处理请求的缓存控制逻辑,与XTNetworkRequest
配合使用,最好不要直接调用XTNetworkRequest
构造请求对象,封装请求参数,子类继承该类,配置对应的参数XTNetworkResponse
构造响应对象,封装响应的状态码,responseData,responseString等,子类可覆写该方法实现自己的有业务处理XTNetworkConfig
网络配置类,存放一些网络请求的通用配置
####使用方法 1. 初始化配置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
// Log config
[XTLogConfig loadConfig];
// Cache path
[XTNetworkConfig defaultConfig].HTTPCachePath = [[XTUtil appDocPath] stringByAppendingPathComponent:@"HTTPCache"];
// Register client
[[XTNetworkEngine defaultEngine] registerClientWithBaseURLString:@"http://www.raywenderlich.com/demos/weather_sample/"];
...
}
2. 发送请求
RayWeatherRequest *request = [[RayWeatherRequest alloc] init];
request.responseClassName = NSStringFromClass([RayWeatherResponse class]);
[request startWithCallback:^(XTNetworkResponse *response) {
RayWeatherResponse *weatherResponse = (RayWeatherResponse *)response;
XTLog(XTL_INFO_LVL, @"ViewController", @"Response:%@ weather:%@", weatherResponse, weatherResponse.weather);
}];
详细代码请参考RayWeather.m
##日志
XTLog
日志
XTLog(level,Category,fmt...)
- **level:**日志等级,取值为
XTL_VERBOSE_LVL
XTL_DEBUG_LVL
XTL_INFO_LVL
XTL_WARN_LVL
XTL_ERROR_LVL
- **category:**模块名,例如@"XTNetwork"
- **fmt...:**日志内容
- 提供XTLogVerbose, XTLogDebug, ...等短方法以方便调用 ###源码片段
...
if (![self validateRequest:request error:&error])
{
...
XTLog(XTL_INFO_LVL, @"ViewController", @"Response:%@ weather:%@", weatherResponse, weatherResponse.weather);
...
}
if (![self validateRequest:request error:&error])
{
...
XTLogInfo( @"ViewController", @"Response:%@ weather:%@", weatherResponse, weatherResponse.weather);
...
}