0

I am trying to call a web service. I tried this

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.75.1:8082/projectname/public/tests"]];
    NSURLSessionConfiguration *configuration = [ NSURLSessionConfiguration ephemeralSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *localFile, NSURLResponse *response, NSError *error) {
        if(!error){
            NSLog(@"no error");

        }else{
            NSLog(@"error");
        }
    }];
    [task resume];
}

as you see there are two nslog statements. I got the no error one.

when I call that web service from my safari, I got a simple string which is index printed in the browser, how can I see that string in my xcode please?

Thanks

CW0007007
  • 5,681
  • 4
  • 26
  • 31
Carol Smith
  • 199
  • 1
  • 1
  • 16

3 Answers3

2

you can implement the delegate method

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location;

EDIT

Try This

NSHTTPURLResponse *response = nil;
NSError *error = nil;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL      URLWithString:YOUR URL]];
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"~~~~~ Status code: %d", [response statusCode]);
//Print your recived data here..
NSString *str = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
NSLog(@"str: %@", str);
Dheeraj Kumar
  • 441
  • 3
  • 16
  • you can refer this link for detail:- *http://hayageek.com/ios-nsurlsession-example/* – Dheeraj Kumar Jul 18 '14 at 12:24
  • I am now to IOS, could you please change my code and tell me what to delete from it ? or I have to keep it as it is and just add your function? also how to print the message inside ur code please? – Carol Smith Jul 18 '14 at 12:28
  • thanks. However, you didn't use nsurlsession, which as I read is very recommended. is that correct plz? – Carol Smith Jul 18 '14 at 12:48
  • NSUrlsession is for downloading file, and here you are not downloading any file just response or string, so it is not nessecery to use NSURlsession.. – Dheeraj Kumar Jul 18 '14 at 13:10
  • +1 for the efforts. However, I found that using `NSSession` is better. – Carol Smith Jul 20 '14 at 20:52
1

You can use the delegate methods. When a NSURLSessionDownlaodTask is completed, it's delegates will be called if your class confirmed to it.

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

you can get your response by parsing the data in that delegate method. It will tell you the location that the URLSession stores the downloaded result.

BalaChandra
  • 632
  • 9
  • 33
  • I am now to IOS, could you please change my code and tell me what to delete from it ? or I have to keep it as it is and just add your function? also how to print the message inside ur code please? lastly, I am using the correct way to call the web service? – Carol Smith Jul 18 '14 at 12:29
1

what I would do If I were you is:

NOTE: it is based on you said you receive a simple string only from your back-end. if that is not a simple string, you may need to revise the –connectionDidFinishLoading: method's body.

.h

@interface UIRandomViewController : UIViewController {

    NSURLConnection *_urlConnection;
    NSMutableData *_receivedData;

    // ...

}

// ...

@end

.m

@implementation UIRandomViewController {

    // ...

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];

        NSURLRequest *_request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.75.1:8082/projectname/public/tests"]];
        _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self startImmediately:TRUE];

        // ...

    }

    // ...

    #pragma mark - <NSURLConnectionDelegate>

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        _receivedData = nil;
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        if (_receivedData == nil) _receivedData = [NSMutableData dataWithData:data];
        else [_receivedData appendData:data];
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSString *_receivedString = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding];
        // hello beautiful...
        NSLog(@"received data : %@", _receivedString);
    }
}

@end
holex
  • 23,961
  • 7
  • 62
  • 76