当前位置 : 主页 > 网页制作 > Nodejs >

使用AFNetWorking+XMLDictionary调用、解析Webservice

来源:互联网 收集:自由互联 发布时间:2021-06-24
列表内容 (1)工具: 1.AFNetworking 3.0 类:AFHTTPSessionManager github 2.XMLDictionary github (2)使用: WebService URL:http://www.webxml.com.cn/WebServices/WeatherWS.asmx 方法:getSupportCityString //获得支持的城市

列表内容

(1)工具:
1.AFNetworking 3.0
类:AFHTTPSessionManager
github
2.XMLDictionary
github

(2)使用:
WebService URL:http://www.webxml.com.cn/WebServices/WeatherWS.asmx

方法:getSupportCityString
//获得支持的城市/地区名称和与之对应的ID
//输入参数:theRegionCode = 省市、国家ID或名称,返回数据:一维字符串数组。

分析:
1.post请求webservice添加请求头和请求体( 包含参数值)及可

2.AFNetWorking使用方法AFHTTPSessionManager
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
progress:(void (^)(NSProgress * _Nonnull))uploadProgress
success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure

请求及解析实例代码:

//URL
NSString *webServiceUrl = @"http://www.webxml.com.cn/WebServices/WeatherWS.asmx";
    //参数值
    NSString *theCityCode = @"nanjing";
    //请求体拼接
    NSString *soapMessage = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                       "<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/\">"
                       "<soap:Body>"
                       "<getSupportCityString xmlns=\"http://WebXml.com.cn/\">"
                       "<theRegionCode>%@</theRegionCode>"
                       "</getSupportCityString>"
                       "</soap:Body>"
                       "</soap:Envelope>",theCityCode];


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //请求头设置
    [manager.requestSerializer setValue:@"www.webxml.com.cn" forHTTPHeaderField:@"Host"];
    [manager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"http://WebXml.com.cn/getSupportCityString" forHTTPHeaderField:@"SOAPAction"];
    //请求体设置
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return soapMessage;
    }];
    //设置返回XMLParser数据解析类型
    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

    [manager POST:webServiceUrl parameters:soapMessage progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        //XMLDictionaryParser解析
        XMLDictionaryParser *parser = [XMLDictionaryParser sharedInstance];
        NSDictionary *data = [parser dictionaryWithParser:responseObject];
        //所有数据
// NSLog(@"Data:%@",data);
        //需要的内容
        NSDictionary *dic = [data valueForKey:@"soap:Body"];
        NSLog(@"Data:\n%@",dic);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"Error:%@",error);
    }];

注:
manager 默认解析方式:AFJSONResponseSerializer

网友评论