0
如何從服務器讀取txt文件內容並在android中使用它?新的android! 這是我的代碼。如何從URL中讀取txt文件並將其用作字符串?
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
/**
* Created by Mukesh on 4/23/2017.
*/
public class PlayerActivity extends Activity {
private String urlStream;
private VideoView myVideoView;
public static void main(String[] args) {
try {
URL url = new URL("http://www.example.com/file.txt");
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
br.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
myVideoView = (VideoView) this.findViewById(R.id.myVideoView);
MediaController mc = new MediaController(this);
myVideoView.setMediaController(mc);
urlStream = ""; //how to keep the text read from txt file from server
runOnUiThread(new Runnable() {
@Override
public void run() {
myVideoView.setVideoURI(Uri.parse(urlStream));
myVideoView.start();
}
});
}
}
此處urlStream =從url中讀取的文本。檢查解決方案無處不在,但無法修復。這是一個M3U8播放器的Android應用程序。 m3u8 url保存在服務器的txt文件中,應用程序應該從txt文件讀取url並將其附加到appStream中的urlStream。
我不想顯示的TextView文本。我想從url中將文本讀取設置爲urlStream =從url中讀取文本或將文本顯示爲Toast –