3

我正在开发一个使用来自 Web 服务的数据的应用程序。我尝试使用 afnetworking 3.x 来使用来自 Web 服务的数据。我卡在某个地方。不知道从哪里开始。希望您的帮助。

   NSString *mainapiKey = [NSString stringWithFormat:@"12345"];
    NSString *urlStringgetairport = [NSString stringWithFormat:@"http://mobileapi.uk/"];
    NSURL *getairportUrl = [NSURL URLWithString:urlStringgetairport];
    NSMutableURLRequest *finalRequest = [NSMutableURLRequest requestWithURL:getairportUrl];
    NSString *soapmessage = [NSString stringWithFormat:@"<?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"
                             "<GetAirport xmlns=\"http://mobileapi.uk/\">\n"
                             "<Authkey>%@</Authkey>\n"
                             "<AirportCode>%@</AirportCode>\n"
                             "</GetAirport>\n"
                             "</soap:Body>\n"
                             "</soap:Envelope>\n"
                             ,mainapiKey, @""];


    NSData *soapdata = [soapmessage dataUsingEncoding:NSUTF8StringEncoding];
    NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapmessage length]];

    [finalRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [finalRequest addValue:@"http://mobileapi.uk/GetAirport" forHTTPHeaderField:@"SOAPAction"];
    [finalRequest addValue:@"mobileapi.uk" forHTTPHeaderField:@"HOST"];
    [finalRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
    [finalRequest setHTTPMethod:@"POST"];
    [finalRequest setHTTPBody:soapdata];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStringgetairport parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"output :%@", responseObject);

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

    }];

webservice输出是一个json

以正常方式成功获取数据

4

3 回答 3

3

更改以下代码

[manager GET:urlStringgetairport parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

   NSLog(@"output :%@", responseObject);

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

}];

到这个代码

manager.responseSerializer = [AFJSONResponseSerializer serializer];//only if your response type is JSON
NSURLSessionDataTask *task = [manager dataTaskWithRequest:finalRequest completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
          NSLog(@"output :%@", responseObject);  
}];
[task resume];

您的代码中的问题是,您创建了 url 请求并且没有使用 AFNetworking API,您为 URL 调用了 GET 方法。仅使用您正在加载的代码 - 意味着将 url 放入浏览器中的相同效果。

于 2015-12-28T05:42:16.723 回答
0

是的,输出是 Json。将该 id 分配responseObjectNSDictionary这样的:

NSDictionary *dict = responseObject;

现在,您可以使用此字典对象提取以下值:

NSString *yourString = [dict valueForKey:@"your-key"];
NSMutableArray *arr = [[NSMutableArray alloc] init];
arr = [dict ObjectForKey:@"your-key"];
于 2015-12-28T05:36:43.227 回答
0

要获取数据,您可以使用以下代码

NSString *mainapiKey = [NSString stringWithFormat:@"12345"];
NSString *urlStringgetairport = [NSString stringWithFormat:@"http://mobileapi.uk/"];
NSURL *getairportUrl = [NSURL URLWithString:urlStringgetairport];
NSMutableURLRequest *finalRequest = [NSMutableURLRequest requestWithURL:getairportUrl];
NSString *soapmessage = [NSString stringWithFormat:@"<?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"
                         "<GetAirport xmlns=\"http://mobileapi.uk/\">\n"
                         "<Authkey>%@</Authkey>\n"
                         "<AirportCode>%@</AirportCode>\n"
                         "</GetAirport>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n"
                         ,mainapiKey, @""];


NSData *soapdata = [soapmessage dataUsingEncoding:NSUTF8StringEncoding];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapmessage length]];

[finalRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[finalRequest addValue:@"http://mobileapi.uk/GetAirport" forHTTPHeaderField:@"SOAPAction"];
[finalRequest addValue:@"mobileapi.uk" forHTTPHeaderField:@"HOST"];
[finalRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[finalRequest setHTTPMethod:@"POST"];
[finalRequest setHTTPBody:soapdata];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//From here replace your code
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

    NSData *data = responseObject;
    NSDictionary *dictTemp = [NSDictionary dictionaryWithXMLData:data]; //XMLDictionary will use to parse xml
    NSLog(@"%@",dictTemp);

}];

[dataTask resume];

如果您的响应在 json 中,则替换manager.responseSerializer = [AFHTTPResponseSerializer serializer];为代码 manager.responseSerializer = [AFJSONResponseSerializer serializer];

于 2016-09-28T10:13:09.297 回答