IOS 7如何打印url响应数据

时间:2014-07-18 12:06:09

标签: ios objective-c ios7

我正在尝试调用Web服务。我试过这个

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

如您所见,有两个nslog语句。我得到了no error一个。

当我从我的safari中调用该Web服务时,我在浏览器中打印了一个简单的字符串index,如何才能在我的xcode中看到该字符串?

由于

3 个答案:

答案 0 :(得分:2)

您可以实现委托方法

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

修改

试试这个

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

答案 1 :(得分:1)

您可以使用委托方法。完成NSURLSessionDownlaodTask后,如果您的班级确认了,则会调用其代理人。

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

您可以通过解析该委托方法中的数据来获取响应。它会告诉您URLSession存储下载结果的位置。

答案 2 :(得分:1)

如果我是你,我会怎么做:

注意:它基于你说你只从你的后端收到一个简单的字符串。如果这不是一个简单的字符串,您可能需要修改–connectionDidFinishLoading:方法的正文。

的.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