2017-01-02 9 views
0

rssフィードを読み込もうとしていますが、BufferedReaderから読み込もうとしたときにエラーが下がりました。java.io.EOFException:BufferedReaderからの読み取り中に nが見つかりません

Error stack trace [screenshot]

フォローRSSフィードライン62

private String downloadXML(String urlPath){ 
     StringBuilder xml = new StringBuilder(); 
     try { 
      URL url = new URL(urlPath); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      int responseCode = connection.getResponseCode(); 
      Log.d(TAG, "downloadXML: The response code was " + responseCode); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

      int charRead; 
      char [] bufferRead = new char[500]; 
      while (true){ 
       charRead = reader.read(bufferRead); 

       if(charRead<0){ 
        break; 
       } 
       if(charRead>0){ 
        xml.append(String.copyValueOf(bufferRead, 0, charRead)); 
       } 
      } 
      reader.close(); 

      return xml.toString(); 
     } 
     catch (MalformedURLException e){ 
      Log.e(TAG, "downloadXML: Invalid URL " + e.getMessage()); 
     } 
     catch (IOException e){ 
      Log.e(TAG, "downloadXML: IO exception " + e.getMessage()); 
      e.printStackTrace(); 
     } 
     catch (SecurityException e) { 
      Log.e(TAG, "downloadXML: SecurityEception " + e.getMessage()); 
     } 
     return null; 
    } 

フィード読み取り、変換された方法はあまり理解されるであろう任意の助けcharRead = reader.read(bufferRead); です。

+0

'while(reader.readLine()!= null)で' while(true) 'を変更しようとしました。 –

+0

同じエラーが発生しました。 –

+0

このコードで行番号を10〜22に変更してください。 'String str; while((str = reader.readLine())!= null){ xml.append(str); } reader.close(); ' –

答えて

0

このプロジェクトでは、このような問題が発生しました。私たちの解決策は、httpヘッダーの設定を追加することです。 addHeader( "Connection"、 "close")