iSh*_*war 3 c# web-services objective-c nsinputstream ios
我想从服务器读取流数据与相应的URL,目前我正在尝试使用NSInputStream来读取数据,但我收到错误"错误2操作无法完成.没有这样的文件或目录".Web开发人员以字节为单位接收数据然后他将该数据转换为Stream,如MemoryStream(byteData)[注意:Web服务是用.net编写的]并且已经将它返回给我.读取这样一种数据的方法是什么,我试过AISHTTPRequest得到大小为0字节的文件,我再次尝试了NSURLConnction我得到了大小为0字节的文件,现在我正在使用NSInputStream并且我得到了提到的ERROR在开始.这是我的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");
}
}
}
Run Code Online (Sandbox Code Playgroud)