0

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

这是我的代码

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 应用程序,但它以单行而不是换行符输出所有内容,因为它在电子表格中。

4

1 回答 1

1

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

 while ((StringBuffer = bufferReader.readLine()) != null) {
            stringText += StringBuffer + "\n";
 }
于 2012-12-05T20:27:35.370 回答