Jump to content

Getting text from a website in Java for Android

RileyTheFox

Hello there. I'm trying to make an Android app for a radio I'm a presenter for. So basically, the music doesn't play on phones when visiting the site. So we need an app for mobile users to listen to. And I've started off making it. On the site, we list the song name and the artist of that song. We also want that in the app. However, the text is blank even though I've copy pasted Oracle's documentation of reading from a website. Here's how the code looks so far: 

 

1e7cb9bf05bedb0960a5473f5430ac10.png

 

000cacf543edb424e3ecdc94198352c6.png

 

 

 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

You don't ever use System.out.println in an Android app. I haven't looked at your code in detail, but generally you parse the website (your current mechanism seems fine, but not necessarily robust), and add that text to something like a TextView widget, rather than using System.out.println.

 

 

System.out.println() is generally used for console applications - something which an Android application isn't.

 

 

 

Edit: I see you're using a TextView with setText, but you're never calling the getSongName() method, so songName = null. Call that function first. I don't think it's necessary to also store it in a global variable. Have the getSongName() method return a string, and in your onCreate(), do something like the following:

 

textView.setText(getSongName())

 

No global variables needed in this case.

 

 

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Tazman192 said:

You don't ever use System.out.println in an Android app. I haven't looked at your code in detail, but generally you parse the website (your current mechanism seems fine, but not necessarily robust), and add that text to something like a TextView widget, rather than using System.out.println.

 

 

System.out.println() is generally used for console applications - something which an Android application isn't.

That was just a test, not using it for a permanent thing. I've tried parsing it using JSoup but that doesn't work either. Nothing shows up at all. The only time I managed the TextView widget to show something was where I hard coded the String in my Java class to have a value. I've successfully made the TextView widget respond to my Java string which is nice, it's just the URL not doing anything :/ 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, EvilCat70 said:

That was just a test, not using it for a permanent thing. I've tried parsing it using JSoup but that doesn't work either. Nothing shows up at all. The only time I managed the TextView widget to show something was where I hard coded the String in my Java class to have a value. I've successfully made the TextView widget respond to my Java string which is nice, it's just the URL not doing anything :/ 

Read my edit to my original post :)

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Tazman192 said:

Read my edit to my original post :)

OH LOL. I'll see what that does xD Wrote the message before you edited so I didn't see it :P I'll reply back with info. 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, EvilCat70 said:

OH LOL. I'll see what that does xD Wrote the message before you edited so I didn't see it :P I'll reply back with info. 

Also, for testing the getSongName() method, use a log to print the song name, not a println(). Plenty of resources available on how to use logs. Bottom line is don't use println() for testing variable values :)

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Tazman192 said:

Read my edit to my original post :)

Nothing still. a3e4c92076fe5fe3182c12abd19c870f.png

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, EvilCat70 said:

Nothing still. a3e4c92076fe5fe3182c12abd19c870f.png

 

Still wrong. Can you pastebin the code and I'll fix it up for you and tell me if it works. I'll explain what I did if it does work :)

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Tazman192 said:

 

Still wrong. Can you pastebin the code and I'll fix it up for you and tell me if it works. I'll explain what I did if it does work :)

https://pastebin.com/YsFLPNcx thanks for your help :)

 

Been doing Java for 2.5 years but only in Bukkit (Minecraft) so not expanded like this before :(

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, EvilCat70 said:

https://pastebin.com/YsFLPNcx thanks for your help :)

 

Been doing Java for 2.5 years but only in Bukkit (Minecraft) so not expanded like this before :(

 

 

package com.flareradio.flareradio;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
 
public class MainMenu extends AppCompatActivity {
 
    String songName;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        TextView textView;
 
        String songName = getSongName();
        if (songName == null) {
            Log.i("MainActivity", "Song name is null - parsing failed.")
        } else {
            textView = (TextView) findViewById(R.id.textView);
            textView.setText(songName);
        }   
    }
 
    public String getSongName() {

        String songHTML = null;
        try {

            // Here we make a URL to the web page
            URL flare = new URL("http://www.api.flare-radio.com/php/song/");

            // Next, we need to get the input stream through the URL connection
            // (you were missing this step)
            URLConnection connection = flare.openConnection();
            InputStream is = connection.getInputStream();


            // Once an input stream is created, the rest is basic Java IO method calling


            BufferedReader in = new BufferedReader(new InputStreamReader(is));

            // Read one line at a time and concatenate to the songHTML string
            while ((parsedLine = in.readLine()) != null) {
                songHTML += parsedLine;
            }   

            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return songHTML;
    }

 
}

 

 

Don't have any IDE for Java on my laptop so may have some code errors (wrote this on Sublime), but run it and let me know if there are any errors.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, EvilCat70 said:

https://pastebin.com/YsFLPNcx thanks for your help :)

 

Been doing Java for 2.5 years but only in Bukkit (Minecraft) so not expanded like this before :(

 

FWIW, the website where you're scraping from does not load for me.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Tazman192 said:

 

 


package com.flareradio.flareradio;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
 
public class MainMenu extends AppCompatActivity {
 
    String songName;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        TextView textView;
 
        String songName = getSongName();
        if (songName == null) {
            Log.i("MainActivity", "Song name is null - parsing failed.")
        } else {
            textView = (TextView) findViewById(R.id.textView);
            textView.setText(songName);
        }   
    }
 
    public String getSongName() {

        String songHTML = null;
        try {

            // Here we make a URL to the web page
            URL flare = new URL("http://www.api.flare-radio.com/php/song/");

            // Next, we need to get the input stream through the URL connection
            // (you were missing this step)
            URLConnection connection = flare.openConnection();
            InputStream is = connection.getInputStream();


            // Once an input stream is created, the rest is basic Java IO method calling


            BufferedReader in = new BufferedReader(new InputStreamReader(is));

            // Read one line at a time and concatenate to the songHTML string
            while ((parsedLine = in.readLine()) != null) {
                songHTML += parsedLine;
            }   

            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return songHTML;
    }

 
}

 

 

Don't have any IDE for Java on my laptop so may have some code errors (wrote this on Sublime), but run it and let me know if there are any errors.

Cannot resolve 'parsedLine' and there's no other variable named that in the entire class 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Tazman192 said:

 

FWIW, the website where you're scraping from does not load for me.

We did turn off our Icecast server for some silenced for what happened in Manchester, but they'll be back on soon. The servers were on earlier, however, but still, wouldn't grab anything. 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, EvilCat70 said:

We did turn off our Icecast server for some silenced for what happened in Manchester, but they'll be back on soon. The servers were on earlier, however, but still, wouldn't grab anything. 

Okay. Regarding the parsedLine reference, add String parsedLine; before the while loop in the try block

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Tazman192 said:

Okay. Regarding the parsedLine reference, add String parsedLine; before the while loop in the try block

4c9f24a94d3fe4d6430f35ab6534fda4.png

Like this? 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, EvilCat70 said:

4c9f24a94d3fe4d6430f35ab6534fda4.png

Like this? 

 

Yeah that should do it.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Tazman192 said:

 

Yeah that should do it.

675def53d67a34963824257a647fef2f.png

Nope :(

The servers are back up too 

 

ba87670cac929cfd5c6ba0f01f9083df.png

 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, EvilCat70 said:

675def53d67a34963824257a647fef2f.png

Nope :(

The servers are back up too 

 

ba87670cac929cfd5c6ba0f01f9083df.png

 

 

Can you add a few Logs in the try block to print out the song name, and ensure it's actually going into the try block? The page still doesn't load for me.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Tazman192 said:

 

Can you add a few Logs in the try block to print out the song name, and ensure it's actually going into the try block? The page still doesn't load for me.

Did just find a few issues with what you sent me, variable 'songName' was never used when it was being sent into the TextView widget, which resulted in it being null (as we didn't even set it). I changed that up. I added a for loop to print the log out 10 times too.

 

442a18a97419f9fd3d342260e78e3dfd.png

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, EvilCat70 said:

Did just find a few issues with what you sent me, variable 'songName' was never used when it was being sent into the TextView widget, which resulted in it being null (as we didn't even set it). I changed that up. I added a for loop to print the log out 10 times too.

 

442a18a97419f9fd3d342260e78e3dfd.png

What do you mean it wasn't being used? It was being set with the line String songName = getSongName(), and that was passed into the setText(). I assume that getSongName() was returning a null string?

 

EDIT: forgot to remove the global songName variable - it must have been referring to that as the unused variable. Does it print the songName in the log correctly?

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Tazman192 said:

What do you mean it wasn't being used? It was being set with the line String songName = getSongName(), and that was passed into the setText(). I assume that getSongName() was returning a null string?

 

EDIT: forgot to remove the global songName variable - it must have been referring to that as the unused variable. Does it print the songName in the log correctly?

720aa2c0fc847fb7a048d20e6791214a.png

It says that.

 

Which responds to line 43, which is

 

 InputStream is = connection.getInputStream();

 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, EvilCat70 said:

720aa2c0fc847fb7a048d20e6791214a.png

It says that.

 

Which responds to line 43, which is

 


 InputStream is = connection.getInputStream();

 

 

Ah ok. Haven't worked with android applications in some time so I forgot about this. You have to create a new thread to run network tasks. If you run it on the main (UI) thread, this will ultimately slow down the UI as connecting to a URL can take time. Hence, create something like an Asynctask and add the code I wrote earlier into that task. An Asynctask is essentially a worker thread designed for things like network connection, so the UI thread is not slowed down.

 

https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception explains how to do this.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Tazman192 said:

 

Ah ok. Haven't worked with android applications in some time so I forgot about this. You have to create a new thread to run network tasks. If you run it on the main (UI) thread, this will ultimately slow down the UI as connecting to a URL can take time. Hence, create something like an Asynctask and add the code I wrote earlier into that task. An Asynctask is essentially a worker thread designed for things like network connection, so the UI thread is not slowed down.

 

https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception explains how to do this.

It has a bunch of RSS feed which I don't need so what do I delete?

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, EvilCat70 said:

It has a bunch of RSS feed which I don't need so what do I delete?

 

package com.flareradio.flareradio;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
 
public class MainMenu extends AppCompatActivity {
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
    }
 
    public String getSongName() {

        String songHTML = null;
        try {

            // Here we make a URL to the web page
            URL flare = new URL("http://www.api.flare-radio.com/php/song/");

            // Next, we need to get the input stream through the URL connection
            // (you were missing this step)
            URLConnection connection = flare.openConnection();
            InputStream is = connection.getInputStream();


            // Once an input stream is created, the rest is basic Java IO method calling


            BufferedReader in = new BufferedReader(new InputStreamReader(is));

            // Read one line at a time and concatenate to the songHTML string
            while ((parsedLine = in.readLine()) != null) {
                songHTML += parsedLine;
            }   

            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return songHTML;
    }



    class DownloadFilesTask extends AsyncTask<Void, Void, String> {
        
         protected String doInBackground(URL... urls) {
            
            String songHTML = null;
            try {

                // Here we make a URL to the web page
                URL flare = new URL("http://www.api.flare-radio.com/php/song/");

                // Next, we need to get the input stream through the URL connection
                URLConnection connection = flare.openConnection();
                InputStream is = connection.getInputStream();


                // Once an input stream is created, the rest is basic Java IO method calling


                BufferedReader in = new BufferedReader(new InputStreamReader(is));

                // Read one line at a time and concatenate to the songHTML string
                while ((parsedLine = in.readLine()) != null) {
                    songHTML += parsedLine;
                }   

                in.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            // The return value is passed as a parameter into onPostExecute
            return songHTML;
        }


        protected void onPostExecute(String result) {
            // Sanity check - Check if the result was passed correclty
            Log.i(TAG, result); 

            // Add the result to the textView. If the Log prints null, this won't work
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText(result);
        }
     }
 

 
}

 

 

You want to add something like the above into your MainActivity class. Again, not tested but the complete code is there (subject to any errors).

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Tazman192 said:

 


package com.flareradio.flareradio;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
 
public class MainMenu extends AppCompatActivity {
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
    }
 
    public String getSongName() {

        String songHTML = null;
        try {

            // Here we make a URL to the web page
            URL flare = new URL("http://www.api.flare-radio.com/php/song/");

            // Next, we need to get the input stream through the URL connection
            // (you were missing this step)
            URLConnection connection = flare.openConnection();
            InputStream is = connection.getInputStream();


            // Once an input stream is created, the rest is basic Java IO method calling


            BufferedReader in = new BufferedReader(new InputStreamReader(is));

            // Read one line at a time and concatenate to the songHTML string
            while ((parsedLine = in.readLine()) != null) {
                songHTML += parsedLine;
            }   

            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return songHTML;
    }



    class DownloadFilesTask extends AsyncTask<Void, Void, String> {
        
         protected String doInBackground(URL... urls) {
            
            String songHTML = null;
            try {

                // Here we make a URL to the web page
                URL flare = new URL("http://www.api.flare-radio.com/php/song/");

                // Next, we need to get the input stream through the URL connection
                URLConnection connection = flare.openConnection();
                InputStream is = connection.getInputStream();


                // Once an input stream is created, the rest is basic Java IO method calling


                BufferedReader in = new BufferedReader(new InputStreamReader(is));

                // Read one line at a time and concatenate to the songHTML string
                while ((parsedLine = in.readLine()) != null) {
                    songHTML += parsedLine;
                }   

                in.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            // The return value is passed as a parameter into onPostExecute
            return songHTML;
        }


        protected void onPostExecute(String result) {
            // Sanity check - Check if the result was passed correclty
            Log.i(TAG, result); 

            // Add the result to the textView. If the Log prints null, this won't work
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText(result);
        }
     }
 

 
}

 

 

You want to add something like the above into your MainActivity class. Again, not tested but the complete code is there (subject to any errors).

'parsedLine' cannot be resolved.

'findViewById' method cannot be resolved.

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×