Webservice 封装为 NSOperation 进行接口访问--IOS
iOS中对于 Webservice 的使用很频繁,在我的项目中我使用继承NSOperation的方式在ViewController 里直接使用进行接口访问,感觉使用起来比较方便,特别总结出来进行分享和讨论。 1.首先要导入ASIHTTPRequest 的全部文件,地址:http://code4app.com/ios/ASIHTTPRequest%E8%B0%83%E7%94%A8WebService/515111226803fa2a10000000 2.创建继承NSOperation的文件 1)MXNWebService.h #import <Foundation/Foundation.h> @protocol MXNWebServiceDelegate <NSObject> -(void)MXNWebServiceNote; @end @interface MXNWebService : NSOperation { ? ? NSMutableData *xmlData; } @property(nonatomic,strong)id<MXNWebServiceDelegate> delegate; @property(nonatomic,strong,readonly)NSString *mark; @property(strong,nonatomic)NSMutableData *xmlData; @property(strong,nonatomic)NSString *result; -(instancetype)initWithMark:(NSString *)mark; -(void)setMobileCode:(NSString *)mobileCode; -(void)setUserID:(NSString *)userID; -(NSString *)getResultJson; //block typedef void (^MXNWebServiceBlock)(); @property(nonatomic,copy) MXNWebServiceBlock mxnWebServiceBlock; @end 2)MXNWebService.m #import <Foundation/Foundation.h> #import "MXNWebService.h" #import "NetWebServiceRequest.h" @interface MXNWebService()<NetWebServiceRequestDelegate> @property(nonatomic,readwrite)NSString *mark; @property(nonatomic,retain)NetWebServiceRequest *runningRequest; @property(nonatomic,retain)NSString *_smethodName; @property(nonatomic,retain)NSString *_smobileCode; @property(nonatomic,retain)NSString *_suserID; @property(nonatomic,retain)NSString *_resultJson; @end @implementation MXNWebService @synthesize runningRequest=_runningRequest; @synthesize xmlData; @synthesize _smethodName; @synthesize _smobileCode; @synthesize _suserID; @synthesize _resultJson; -(instancetype)initWithMark:(NSString *)mark { ? ? self=[superinit]; ? ? if(self) ? ? { ? ? ? ? self.mark=mark; ? ? } ? ? returnself; } -(void)setmethodName:(NSString *)methodName ?//设置方法名 { ? ? self._smethodName=methodName; } -(void)setMobileCode:(NSString *)mobileCode ? ?//根据不同的方法设置不同的参数,通过这一个类基本可以实现整个项目的WebService访问 { ? ? self._smobileCode=mobileCode; } -(void)setUserID:(NSString *)userID { ? ? self._suserID=userID; } -(NSString *)getParamList//此处可以设置对不同的方法设置不同的参数列表 { ? ? NSString *list; ? ? if ([_smethodNameisEqualToString:@"getMobileCodeInfo"]) { ? ? ? ? list=[NSStringstringWithFormat: ? ? ? ? ? ? ? @"<mobileCode>%@</mobileCode>n" ? ? ? ? ? ? ? "<userID>%@</userID>n" ? ? ? ? ? ? ?,_smobileCode,_suserID]; ? ? } ?? ? ? ? return list; } -(void)main { ?? ? ? ? NSString *soapMessage_start=[NSStringstringWithFormat: ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"<?xml version="1.0" encoding="utf-8"?>n" ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">n" ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<soap:Body>n" ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%@ xmlns="http://WebXml.com.cn/">n" ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?,_smethodName]; ?? ? ? ? NSString *soapMessage_param=[selfgetParamList]; ?? ? ? ? NSString *soapMessage_end=[NSStringstringWithFormat: ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"</%@>n" ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "</soap:Body>n" ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "</soap:Envelope>n",_smethodName]; ? ? NSString *soapMessage=[[soapMessage_startstringByAppendingString:soapMessage_param]stringByAppendingString:soapMessage_end]; ? ? //NSLog(@"%@",soapMessage); ? ?? ? ? //请求发送到的路径 ? ? NSString *url =@"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; ? ? NSString *soapActionURL = [@"http://WebXml.com.cn/"stringByAppendingString:_smethodName]; ? ? NSLog(@"soapActionURL:%@",soapActionURL); ? ? NetWebServiceRequest *request = [NetWebServiceRequestserviceRequestUrl:urlSOAPActionURL:soapActionURLServiceMethodName:_smethodNameSoapMessage:soapMessage]; ?? ? ? ? [request startAsynchronous]; ? ? [request setDelegate:self]; ? ? self.runningRequest = request; } #pragma mark NetWebServiceRequestDelegate Methods - (void)netRequestStarted:(NetWebServiceRequest *)request { ? ? NSLog(@"Start"); }
- (void)netRequestFinished:(NetWebServiceRequest *)request ? ? ? finishedInfoToResult:(NSString *)result ? ? ? ? ? ? ? responseData:(NSData *)requestData{ ? ? NSLog(@"%@:返回结果%@",_smethodName,result); ? ? self._resultJson=result; ?? ? ?? ? ? ? if (self.mxnWebServiceBlock) { ? ? //通过设置block实现异步加载后对调用它的视图控制器里进行界面的更新或其它操作 ?? ? ? ? ? ? ? ? self.mxnWebServiceBlock(); ? ? } } - (void)netRequestFailed:(NetWebServiceRequest *)request didRequestError:(NSError *)error{ ? ? CLog(@"%@:%@",_smethodName,error); ? ? self._resultJson=@"MXNError"; ?? ? ?? ? ? ? if (self.mxnWebServiceBlock) { ?? ? ? ? ? ? ? ? self.mxnWebServiceBlock(); ? ? } ?? ? }
-(NSString *)getResultJson { ?? ? ? ? return_resultJson; } @end 3.调用这个NSOperation #import "AppDelegate.h" #import "MXNWebService.h" #import "NetWebServiceRequest.h" ? ? NSOperationQueue *mxnQueue; ? ? mxnQueue=[[NSOperationQueuealloc]init]; ? ? mxnQueue.name=@"mxn"; ? ? mxnQueue.maxConcurrentOperationCount=2; ?? ? ? ? MXNWebService *mxnWeb=[[MXNWebServicealloc]initWithMark:@"mxnweb"];? ? ? [mxnWeb setmethodName:@"getMobileCodeInfo"]; ? ? [mxnWeb setMobileCode:@"15022512043"]; ? ? [mxnWeb setUserID:@""]; ?? ? ? ? [mxnQueue addOperation:mxnWeb]; ? ? mxnWeb.delegate=self; ? ? mxnWeb.mxnWebServiceBlock=^(){ ? ? ? ? NSString *result=[mxnWebgetResultJson]; ? ? ? ? //此处根据异步返回的结果进行操作 ? ? }; 这样使用起来感觉比较方便,有更简便的方法欢迎讨论交流。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |