How can I settext to a textview from a file on my server using http

106 Views Asked by At

Trying to learn how to display text that changes every few minutes in a file on a remote web server (served via http). For the time being, I just want to learn how to read from a text file. Below is my MainActivity in my learning app. I know it's ugly but I'm still learning. I would wrap it in code tags if I knew how, sorry in advance

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);  
    }
}
0

There are 0 best solutions below