Android Open Source - GarageRadio Download Title






From Project

Back to project page GarageRadio.

License

The source code is released under:

Apache License

If you think the Android project GarageRadio listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Class DownloadTitle is the thread to refresh the title each SECOND_TITLE_REFRESH seconds
 * in the user interface of the main screen
 */*from www  . j  a  v a  2 s .  co m*/
 * @author Sara Craba
 * @since 4/27/14
 * @version 2.0
 */
package com.garageradio.app.mainpage;

import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import static java.lang.Thread.sleep;

/**
 * @see AsyncTask on Android
 */
public class DownloadTitle extends AsyncTask<String, String, Void>
{
    public AsyncResponse delegate=null;

    /**
     * @see AsyncTask - doInBackground on Android
     *
     * @param url html page to get the title
     */
    protected Void doInBackground(String... url)
    {
        String response = "";

        // Checking for the new title
        while(MainActivity.refreshTitle)
        {
            // Http connection
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url[0]);

            try
            {
                // Getting html page
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";

                // Building the string
                while ((s = buffer.readLine()) != null)
                {
                    response += s;
                }

            }
            catch (Exception e)
            {
                Log.e(MainActivity.DOWNLOAD_TITLE, e.toString());
            }

            // Catting the title part of the html page
            String cattedString= response.substring(response.indexOf("song")+8, response.indexOf("url")-3);

            // Decoding string
            String decodedCattedString=  Html.fromHtml(cattedString).toString();

            // Refreshing text view
            if(decodedCattedString.contains("- "))
            {
                String[] splitTitleFromArtist = decodedCattedString.split("- ");
                String titleSong = splitTitleFromArtist[0];
                String artistSong = splitTitleFromArtist[1];

                publishProgress(titleSong + '\n'+ artistSong);
            }
            else
            {
                publishProgress(decodedCattedString);
            }

            // Sleeping SECOND_TITLE_REFRESH seconds
            try
            {
                sleep(MainActivity.SECOND_TITLE_REFRESH *1000);
            }
            catch (InterruptedException e)
            {
                Log.e(MainActivity.DOWNLOAD_TITLE, e.toString());
            }

            // Cleaning response string
            response = "";

        }
        return null;
    }

    /**
     * @see AsyncTask - onProgressUpdate on Android
     */
    protected void onProgressUpdate(String... title)
    {
        // Refreshing the user interface title texview
        delegate.refreshTitleView(title[0]);
    }

}




Java Source Code List

com.garageradio.app.mainpage.AsyncResponse.java
com.garageradio.app.mainpage.DownloadTitle.java
com.garageradio.app.mainpage.MainActivity.java
com.garageradio.app.palimpsest.DownloadPalimpsest.java
com.garageradio.app.palimpsest.PalimpsestActivity.java
com.garageradio.app.palimpsest.PalimpsestRow.java
com.saracraba.garageradio.ApplicationTest.java
com.saracraba.garageradio.DownloadTitle.java
com.saracraba.garageradio.FirstPageActivity.java
com.saracraba.garageradio.MainActivity.java
com.saracraba.garageradio.MediaPlayerManager.java
com.saracraba.garageradio.NetworkManager.java
com.saracraba.garageradio.NotificationManager.java