微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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];

        //此处根据异步返回的结果进行操作

    };


这样使用起来感觉比较方便,有更简便的方法欢迎讨论交流。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐