ASIFormDataRequest 네트워크 에서 요청 한 간단 한 패키지 soap 프로 토 콜

3818 단어 아이 폰 개발
DataRequest. h 파일
/  Copyright (c) 2013  dong. All rights reserved.
//

#import 

@interface DataRequest : NSObject
+(void)dataWithArr:(NSMutableArray *)arr andRequestType:(NSString *)requestType andRequestName:(NSString *)requestName andBlock:(void(^)(NSString *requestStr))block;
+(NSMutableDictionary *)jsonValue :(NSString *)str;
@end

DataRequest. m 파일
//  Copyright (c) 2013  dong. All rights reserved.
//
//add into  .pch             ,
#define WEBSERVER @"https://XXXXXXXXXXXXXX"
#define KEYVALUE @"XXXXXXXXXXXXXX"

#import "DataRequest.h"
#import "ASIFormDataRequest.h"
#import "GDataXMLNode.h"
@implementation DataRequest
+(void)dataWithArr:(NSMutableArray *)arr andRequestType:(NSString *)requestType andRequestName:(NSString *)requestName andBlock:(void(^)(NSString *requestStr))block{
    
    NSString * str = @"";
    for (int i = 0; i< [arr count]; i++) {
        str = [str stringByAppendingString:[NSString stringWithFormat:@"%@",i,[arr objectAtIndex:i],i]];
    }
    NSString *soapMsg = [NSString stringWithFormat:
                         @""
                         ""
                         ""
                         ""
                         "%@"
                         "%@"
                         "%@>"
                         ""
                         "",requestName,KEYVALUE,str,requestName];
    NSURL *url = [NSURL URLWithString: WEBSERVER];
    ASIFormDataRequest * request=[ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:requestType];
    [request appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
    [request addRequestHeader:@"Content-Type" value:@"application/soap+xml; charset=utf-8"];
    NSLog(@"%@",soapMsg);

    [request setRequestMethod:requestType];
    
    [request setCompletionBlock:^{
        //xml  
        GDataXMLDocument * doc=[[GDataXMLDocument alloc]initWithData:[request responseData] options:0 error:nil];
        //    
        NSString *path=[NSString stringWithFormat:@"/env:Envelope/env:Body/ns1:%@Response",requestName];
        //            
        NSArray *array=[doc nodesForXPath:path error:nil];
        NSString *responseStr = nil;
        for (GDataXMLElement *element in array) {
            //  return     
            NSArray * titleArray=[element elementsForName:@"return"];
            if ([titleArray count] != 0) {
                GDataXMLElement * ele=[titleArray objectAtIndex:0];
                //       
                responseStr=[ele stringValue];
            }
        }
        block(responseStr);
    }];
    
    [request setFailedBlock:^{
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"  " message:@"    " delegate:self cancelButtonTitle:@"  " otherButtonTitles:nil];
        [alert show];
        [alert release];
        block(@"failed");
    }];
    
    [request startAsynchronous];
}
+(NSMutableDictionary *)jsonValue :(NSString *)str{
    //json  
    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
}

@end

호출:
 NSMutableArray *muArr=[[NSMutableArray alloc] initWithObjects:@"X",@"XX",@"XX", nil];
    [DataRequest dataWithArr:muArr andRequestType:@"POST" andRequestName:@"XX" andBlock:^(NSString *requestStr) {
        NSLog(@"%@",[DataRequest jsonValue:requestStr]);
    }];

좋은 웹페이지 즐겨찾기