/XTProject

Foundation for refactor

Primary LanguageObjective-C

XTProject

##结构图 XTNetwork structure ##网络

  • XTNetworkEngineHTTP请求底层能力处理类,封装了请求参数构造和响应数据的处理,处理请求的缓存控制逻辑,与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);
	...
}