2012-12-05 47 views
0

我有這個代碼,它以文本格式從網上提取日期。

如何在每一行的新行中獲取settext輸出?

這裏是我的代碼

package com.zv.android; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class ZipActivity extends Activity { 

    TextView textMsg, textPrompt; 
    final String textSource = "https://docs.google.com/spreadsheet/pub?key=0iojoI1OogsdiojdsiosdSdzZlbmZqOHdijoQkE&single=true&gid=0&range=A2%3AA200&output=txt"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     textPrompt = (TextView)findViewById(R.id.textprompt); 
     textMsg = (TextView)findViewById(R.id.textmsg); 

     textPrompt.setText("Wait..."); 

     URL CPSE; 
     try { 
      CPSE = new URL(textSource); 
      BufferedReader bufferReader = new BufferedReader(new InputStreamReader(CPSE.openStream())); 
      String StringBuffer; 
      String stringText = ""; 
      while ((StringBuffer = bufferReader.readLine()) != null) { 
       stringText += StringBuffer; 
      } 
      bufferReader.close(); 
      textMsg.setText(stringText); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      textMsg.setText(e.toString()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      textMsg.setText(e.toString()); 
     } 

     textPrompt.setText("Finished!"); 

    } 
} 

我對谷歌電子表格文本文件。它會自動更新。

我能夠將數據提取到android應用程序,但它在單行中輸出所有內容,而不是換行符,因爲它在電子表格中。

+2

stringText + =的StringBuffer + 「\ n」; – dymmeh

+0

謝謝....你真棒我有沒有做過一段時間的Java現在:) – Mowgli

+0

我怎樣才能讓它刷新新數據每次我啓動應用程序,因爲它不刷新更快我關閉應用程序通過回家按鈕。 – Mowgli

回答

1

追加換行符\n每行後你讀

while ((StringBuffer = bufferReader.readLine()) != null) { 
      stringText += StringBuffer + "\n"; 
}