我有这个代码,用于我正在开发的 android 应用程序:
package com.exercise.AndroidInternetTxt;
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 AndroidInternetTxt extends Activity {
TextView textMsg, textPrompt, textSite;
final String textSource = "http://www.xxx/s.php";
@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);
textSite = (TextView)findViewById(R.id.textsite);
//textPrompt.setText("Asteapta...");
URL textUrl;
try {
textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.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("Terminat!");
}
}
它工作正常,它从 .php 文件输出文本。我希望它每 10 秒自动刷新一次,但我真的不知道该怎么做。你能帮我解决这个问题吗?谢谢!