0

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);  
    }
}
Brahma Datta
  • 1,102
  • 1
  • 12
  • 20
  • What problem are you facing? – uneq95 Mar 09 '19 at 09:32
  • Possible duplicate of [Android - How can I read a text file from a url?](https://stackoverflow.com/questions/38372571/android-how-can-i-read-a-text-file-from-a-url) – uneq95 Mar 09 '19 at 09:36
  • Using the variable set within the try from outside the try. I have since went a different route with this but would still like to know how to do this for future reference. – William W Valentine Mar 10 '19 at 10:03

0 Answers0