如何在每一行的新行中获取settext输出?

时间:2012-12-05 20:18:29

标签: java android

我有这个代码,它以文本格式从网上获取日期。

这是我的代码

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!");

    }
}

我在Google电子表格上有文字文件。它会自动更新。

我能够将数据提取到Android应用程序,但它会在电子表格中以单行代替换行输出所有内容。

1 个答案:

答案 0 :(得分:1)

在您阅读的每一行后附加换行符\n

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