尝试学习如何在远程 Web 服务器(通过 http 服务)上的文件中显示每隔几分钟更改一次的文本。目前,我只想学习如何从文本文件中读取。下面是我的学习应用程序中的 MainActivity。我知道这很丑,但我还在学习。如果我知道怎么做,我会把它包装在代码标签中,提前抱歉
package com.mycompany.myapp3;
import android.app.*;
import android.os.*;
import android.widget.*;
import java.net.*;
import java.io.*;
import android.widget.TextView;
public class MainActivity extends Activity
{
String str = null;
// Change the text
public void textviewsongset (){
TextView textView = (TextView)findViewById(R.id.songtextview);
textView.setText("Now Playing: "+str);
}
// Get the currently playing song title
public void getCurrentSong(){
try {
// Create a URL for the desired page
URL url = new URL("http://adovex.com/test.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str = in.readLine();
in.close();
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getCurrentSong();
TextView textView = (TextView)findViewById(R.id.songtextview);
textView.setText("Now Playing: "+str);
}
}