我想从服务器读取带有相应URL的流数据,目前我正在尝试使用NSInputStream来读取数据,但是我收到错误“错误2操作无法完成。没有这样的文件或目录” 。 Web开发人员以字节为单位接收数据然后他将该数据转换为Stream,如MemoryStream(byteData)[注意:Web服务是用.net编写的]并且已经将它返回给我。 读取这种数据的方法是什么,我试过AISHTTPRequest得到大小为0字节的文件,我再次尝试了NSURLConnction我得到了大小为0字节的文件,现在我是使用NSInputStream,我得到了在开始时提到的错误。这是我的NSInputStream代码,
请指导我是否可以阅读此类数据是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");
}
}
}
答案 0 :(得分:5)
它全部使用NSUrlConnection下载流数据。它得到了解决。