如何从web服务获取类型流的数据

时间:2021-03-23 16:24:19

I want to read the stream data from server with corresponding URLs, currently i am trying with NSInputStream for reading the data , But i am getting the error "Error 2 The operation couldn’t be completed. No such file or directory". The web developer receiving the data in bytes formate then he converted that data to Stream like MemoryStream(byteData)[NOTE:Web services are written in .net] and have returned the same to me. What is the way to read such a kind of data, I tried the AISHTTPRequest got the file of size 0 bytes, I tried the NSURLConnction again i got file of size 0 bytes, Now I am using the NSInputStream and I am getting the ERROR mentioned at the starting. Here is my code for NSInputStream,

我想用相应的url从服务器读取流数据,目前我正在尝试使用NSInputStream来读取数据,但是我得到了错误的“error 2”操作无法完成。没有这样的文件或目录"。web开发人员以字节形式接收数据,然后将数据转换为流,就像MemoryStream(byteData)[注意:web服务是在。net中编写的],并将数据返回给我。读取这样一种数据的方式是什么,我尝试了AISHTTPRequest的文件大小为0字节,我再次尝试了nsurlconnfunction我得到了0字节的文件,现在我使用NSInputStream,我得到了开始时提到的错误。这是我的NSInputStream代码,

Please guide me whether we can read this kind of data is iOS as a front end, If Yes Then guide for the way that how should i interact with this kind of data. IF the NSInputStream is the intended way to interact with then whats the problem in my code written below.

请指导我,我们是否可以把iOS作为前端,如果可以,我该如何与这类数据进行交互。如果NSInputStream是打算与之交互的方式,那么我下面写的代码中的问题是什么呢?

- (void)setUpStreamForFile {

    NSString *urlStr = @"http://192.168.1.201/example/example.svc/example?parameter={\"DCU_DocId\":\"05e24018-b728-4ec8-9848-d6cae02bec95\"}";

    // iStream is NSInputStream instance variable
    iStream = [[NSInputStream alloc] initWithFileAtPath:urlStr];
    [iStream setProperty:[NSDictionary dictionaryWithObjectsAndKeys:(id) kCFBooleanFalse,    kCFStreamSSLValidatesCertificateChain, nil ] forKey:(NSString *) kCFStreamPropertySSLSettings];
    [iStream setDelegate:self];
    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                       forMode:NSDefaultRunLoopMode];
    [iStream open];
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
    _data = [[NSMutableData alloc] init];
    int bytesRead=0;

    switch(eventCode) {
        case NSStreamEventHasBytesAvailable:
        {
            if(!_data) {

            }
            uint8_t buf[20480];
            unsigned int len = 0;
            len = [(NSInputStream *)stream read:buf maxLength:20480];
            if(len) {
                [_data appendBytes:(const void *)buf length:len];
                // bytesRead is an instance variable of type NSNumber.
                //[bytesRead setIntValue:[bytesRead intValue]+len];
                bytesRead = bytesRead +len;
                NSLog(@"bytesRead:%d",bytesRead);
            }

            else {
                NSLog(@"no buffer!");
            }

            break;
        }
        case NSStreamEventEndEncountered:
        {
            [stream close];
            [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
                              forMode:NSDefaultRunLoopMode];

            stream = nil; // stream is ivar, so reinit it
            break;
        }
        case NSStreamEventErrorOccurred:
        {
            NSError *theError = [stream streamError];
            UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"Error %i: %@",[theError code], [theError localizedDescription]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            NSLog(@"Error %i %@",[theError code], [theError localizedDescription]);
            [theAlert show];

            [stream close];

            break;
        }
        case NSStreamEventOpenCompleted:
        {
            NSLog(@"EventOpen");
        }
        case NSStreamEventHasSpaceAvailable:
        {
             NSLog(@"EventHasSpac");
        }
        case NSStreamEventNone:
        {
            NSLog(@"EventNone");
        }
       }
}

1 个解决方案

#1


5  

Its all using the NSUrlConnection to download the streamed data. It get solved.

它使用NSUrlConnection下载流数据。它解决了。

#1


5  

Its all using the NSUrlConnection to download the streamed data. It get solved.

它使用NSUrlConnection下载流数据。它解决了。