2013-10-10 51 views
3

我想從服務器讀取流數據與相應的URL,目前我正在嘗試用NSInputStream讀取數據,但我得到的錯誤「錯誤2操作couldn沒有完成,沒有這樣的文件或目錄「。 Web開發人員以字節格式接收數據,然後他將數據轉換爲Stream,如MemoryStream(byteData)[注意:Web服務以.net寫入],並已將其返回給我。 什麼是讀取這樣的數據類型,我試過AISHTTPRequest得到了大小爲0字節的文件,我再次嘗試了NSURLConnction我得到了大小爲0字節的文件,現在我正在使用NSInputStream,我得到在起始處提到的ERROR。這裏是我的NSInputStream代碼,如何從Web服務獲取類型流的數據

請指引我,我們是否可以讀取該類型的數據是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

我建議使用JSON進行iOS和.Net Web服務之間的通信。這將迫使.Net開發人員以iOS應用程序中可讀的JSON格式對字節流進行序列化,如base64 – Lanorkin

回答

5

所有使用NSURLConnection的下載流數據。它得到解決。

相關問題