设置开发环境与生产环境的地址开关,结合AFNetworking进行封装,我直接用我以前实现的代码先大致说明下实现后的效果。
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.4.255.255
以此接口我们设定服务器方给我们的基本地址是http://int.dpool.sina.com.cn,这个地址是生产环境的,如果需要改为开发环境也就是说只改这个地址即可,后面跟着的地址参数等无需变动
iplookup/iplookup.php可作为请求数据的地址参数
format=json&ip=218.4.255.255 可作为请求的两个参数
所以我们先简单实现.h文件的设置
#import
#import "AFNetworking.h"
#import "BaseModel.h"
@interface WebServiceHelper : NSObject
//新浪的ip获取地址
//http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.4.255.255
// 0 - 正式服 1 - 测试服
#define DEBUGMODE 0
#if DEBUGMODE
#define kHTTPREQUEST_BASE_URL @"http://int.dpool.sina.com.cn"//测试环境
#else
#define kHTTPREQUEST_BASE_URL @"http://int.dpool.sina.com.cn"//生成环境网络请求
#endif
#pragma mark --------------------------------返回带有对象控制权的model数据---------------
/**
* 请求带有对象控制器的字典Model
*
* @param url 地址
* @param parameters 上传的参数
* @param modelClass Model类型
* @param requester 对象所有者
* @param key 属性名
* @param success 成功返回字典Model数据
* @param failure 失败
*/
+ (void)postAndGetJsonModelWithURLString:(NSString *)url
parameters:(NSDictionary *)parameters
modelClass:(Class)modelClass
requester:(id)requester
key:(NSString *)key
success:(void (^)(BaseModel *baseModel))success
failure:(void (^)(NSError *error))failure;
@end
关于.m的实现我就不一一解释了,详情看代码
#import "WebServiceHelper.h"
#define CustomErrorDomain @"com.WebServiceHelper"
typedef enum
{
PARAMETER_ERROR = 0,//参数错误
EXCEPTION_DESCRIPTION, //网络错误
INTERFACE_CHECKFAIL,//接口验证失败
BC_CODEFAIL//渠道码不存在
}CustomErrorFailed;// 用于表示错误类型
@implementation WebServiceHelper
#pragma mark 请求最基本数据
+(void)postWithURLString:(NSString *)url
parameters:(NSDictionary *)parameters
success:(void (^)(id))success
failure:(void (^)(NSError *))failure
{
NSString *postUrl = [NSString stringWithFormat:@"%@/%@",kHTTPREQUEST_BASE_URL,url];
//需要提交的参数(原参数加加密验证的数据)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
manager.requestSerializer.timeoutInterval = 30;
//因为这个测试的地址需要用GET方法获取,所以这里我先用GET的方法调用了
[manager GET:postUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
if (success)
{
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
#pragma mark ------------------------------请求model数据--------------------------
#pragma mark 请求model数据
+ (void)postAndGetJsonModelWithURLString:(NSString *)url
parameters:(NSDictionary *)parameters
modelClass:(Class)modelClass
success:(void (^)(BaseModel *baseModel))success
failure:(void (^)(NSError *error))failure
{
// 检查需要返回的model类型是否存在
if (modelClass == nil)
{
if (failure != nil)
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"is a parameter error" forKey:NSLocalizedDescriptionKey];
NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:PARAMETER_ERROR userInfo:userInfo];
failure(aError);
NSLog(@"\n\nmodelClass参数 == nil\n\n");
}
return ;
}
[WebServiceHelper postWithURLString:url parameters:parameters success:^(id responseObject) {
// 检查data
if (![responseObject isKindOfClass:[NSDictionary class]])
{
NSLog(@"\n\njsonModelRequestWithInfo请求的数据不是字典类型: data is invalid\n\n");
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"数据不是字典类型" forKey:NSLocalizedDescriptionKey];
NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:PARAMETER_ERROR userInfo:userInfo];
failure(aError);
}
else
{
// 正常返回
BaseModel *baseModel = [[modelClass alloc] initWithDataDic:responseObject];
if (success != nil)
{
success(baseModel);
}
}
} failure:^(NSError *error) {
failure(error);
}];
}
#pragma mark ------------------------------请求有对象所有选的model数据--------------------------
#pragma mark 请求有对象所有选的model数据
+ (void)postAndGetJsonModelWithURLString:(NSString *)url
parameters:(NSDictionary *)parameters
modelClass:(Class)modelClass
requester:(id)requester
key:(NSString *)key
success:(void (^)(BaseModel *baseModel))success
failure:(void (^)(NSError *error))failure
{
// 检查实参
if (requester == nil || key == nil)
{
if (failure != nil)
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"请求的参数requester == nil, or key ==nil" forKey:NSLocalizedDescriptionKey];
NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:PARAMETER_ERROR userInfo:userInfo];
failure(aError);
}
return ;
}
[WebServiceHelper postAndGetJsonModelWithURLString:url parameters:parameters modelClass:modelClass success:^(BaseModel *baseModel) {
//kvo设置属性值
[requester setValue:baseModel forKey:key];
if (success != nil) {
success(baseModel);
}
} failure:^(NSError *error) {
if (error != nil) {
// //如果网络请求失败,则调用一下pageNum的值减1,防止pullUp失败时也会让pageNum加1
// if ([requester isKindOfClass:[BaseViewController class]]) {
// [requester reducePageNum];
// }
failure(error);
}
}];
}
@end
#import "FriendViewController.h"
#import "FriendIPModel.h"
@interface FriendViewController ()
@property (nonatomic,strong)FriendIPModel *friendModel;
@property (weak, nonatomic) IBOutlet UITextField *textFied;
@property (weak, nonatomic) IBOutlet UITextView *containTextV;
@end
@implementation FriendViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"好友";
}
#pragma mark ---------Actions--------
- (IBAction)resetAction {
self.textFied.text = @"53.223.108.40";
self.containTextV.text = @"";
[self.textFied resignFirstResponder];
}
- (IBAction)searcheAction {
[self requestData];
self.containTextV.text = @"";
[self.textFied resignFirstResponder];
}
#pragma mark 请求网路
- (void)requestData
{
[super requestData];
NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
[muDic setObject:self.textFied.text forKey:@"ip"];
[muDic setObject:@"json" forKey:@"format"];
//key值必须和上面定义的属性名一致
[WebServiceHelper postAndGetJsonModelWithURLString:@"iplookup/iplookup.php" parameters:muDic modelClass:[FriendIPModel class] requester:self key:@"friendModel" success:^(BaseModel *baseModel) {
[self requestSuccess];
self.containTextV.text = self.friendModel.country;
} failure:^(NSError *error) {
self.isLoading = NO;
self.containTextV.text = @"请求数据错误";
}];
}