2014-07-24 117 views
4

我想從IHTTPSession.getInputStream()使用下面的代碼讀取InputStream,但它每次都給出Socket TimeOut異常。讀取輸入流的NANOHTTPD給出套接字超時異常

private String readInStream(InputStream in){ 

     StringBuffer outBuffer=new StringBuffer(); 
     BufferedInputStream bis=new BufferedInputStream(in); 
     try { 
      while(bis.available()>0){ 
       int ch= bis.read(); 
       outBuffer.append((char)ch); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length()); 
     return outBuffer.toString(); 
    } 

我也嘗試以下方法,但同樣引發異常

private String readInStream(InputStream in){ 
     String line=""; 
     StringBuffer outBuffer=new StringBuffer(); 

     BufferedReader rd=new BufferedReader(new InputStreamReader(in)); 

     try { 
      while((line=rd.readLine()) != null){ 
       outBuffer.append(line); 
      } 
     } catch (IOException e) { 
      Log.e("IOException", "IOException in readInStream:"); 
      e.printStackTrace(); 
     } 

     Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length()); 
     return outBuffer.toString(); 
    } 

回答

3

充分利用header.And閱讀內容長度高達的CONTENTLENGTH解決了這個問題。

相關問題