3

I'm working on a app that consume data from a web service.I tried to consume data from webservice using afnetworking 3.x. I have stucked in some place.don't know wher to go from here.hope your help.

   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 output is a json

got the data successfully in normal way

Jobs
  • 269
  • 2
  • 6
  • 21

3 Answers3

3

Change following code

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

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

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

}];

To this code

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

The problem in your code is, you created the url request and not using with AFNetworking API, you ergots calling a GET method for the URL. With that code only that your is loading- means same effect of putting the url in a browser.

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
  • this gives this warning `/62:32: Incompatible pointer types assigning to 'AFHTTPResponseSerializer * _Nonnull' from 'AFJSONRequestSerializer * _Nonnull'` – Jobs Dec 28 '15 at 05:45
  • Not getting any warning for me, is it working properly once you run the code? – Johnykutty Dec 28 '15 at 06:08
  • sorry its my problem. but output is null here. – Jobs Dec 28 '15 at 06:33
  • also print following things too and let me know the output NSLog(@"Raw response MIMEType %@",response.MIMEType); NSLog(@"Raw response textEncodingName %@",response.textEncodingName); – Johnykutty Dec 28 '15 at 06:39
  • I checked with these, all null – Jobs Dec 28 '15 at 06:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99097/discussion-between-johnykutty-and-subash). – Johnykutty Dec 28 '15 at 06:48
0

Yes, the output is in Json. Assign that id responseObject to a NSDictionary like this:

NSDictionary *dict = responseObject;

Now, you use this dictionary object to pull out values like :

NSString *yourString = [dict valueForKey:@"your-key"];
NSMutableArray *arr = [[NSMutableArray alloc] init];
arr = [dict ObjectForKey:@"your-key"];
Bista
  • 7,869
  • 3
  • 27
  • 55
  • no, my point is it does not go through success area, it goes through area. in the previous afnetworking, there is afhttprequestopertation. by using this ,can call to a request.in here this does not call to the request. why? what shout I use with `GET:` – Jobs Dec 28 '15 at 05:40
  • Sorry I misunderstood it, [check this](http://stackoverflow.com/questions/21308227/how-can-you-use-afnetworking-or-sthttprequest-to-make-a-request-of-a-soap-web-se/21308267#21308267) – Bista Dec 28 '15 at 07:31
0

For getting data you can use following code

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

If your response is in json then replace manager.responseSerializer = [AFHTTPResponseSerializer serializer]; with code manager.responseSerializer = [AFJSONResponseSerializer serializer];

Indrajeet
  • 5,490
  • 2
  • 28
  • 43