Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Android read file from url

Android read file from url

Scheduled Pinned Locked Moved Android
android
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #1

    I created android application to stream online radio stations but it doesn't work.I want to read ip address from url and assign it to string and when user selects radio to listen I should add port to that string!!!

    public class BackgroundService extends Service implements OnCompletionListener
    {
    MediaPlayer mediaPlayer;
    private String STREAM_URL;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public void onCreate()
    {
        final StringBuilder text = new StringBuilder();
    
        new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    URL url = new URL("http://audiophileradio.stream/Ip.txt");
    
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
    
                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String str;
                    while ((str = in.readLine()) != null)
                    {
                        text.append(str);                      
                    }
                    in.close();
                    text.insert(text.length(), ":8000");
                }
                catch (Exception e)
                {
                    Log.d("MyTag",e.toString());
                }
            }
        }).start();
    

    SharedPreferences sharedPreferences =
    PreferenceManager.getDefaultSharedPreferences(this);
    String radio = sharedPreferences.getString("station", "8000");

        if (radio != null && radio.equals("8000"))
        {
            text.append(":8000");
            STREAM\_URL = text.toString();
            Toast.makeText(this,STREAM\_URL,Toast.LENGTH\_SHORT).show();
        }
        if (radio != null && radio.equals("8010"))
        {
            text.append(":8010");
            STREAM\_URL = text.toString();
        }
        if (radio != null && radio.equals("8020"))
        {
            text.append(":8020");
            STREAM\_URL = text.toString();
        }
        if (radio != null && radio.equals("8030"))
        {
            text.append(":8030");
            STREAM\_URL = text.toString();
        }
    
        mediaPlayer = new MediaPlayer();
        try
        {
            mediaPlayer.setDataSource(STREAM\_URL);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    
        mediaPlayer.setOnCo
    
    D 1 Reply Last reply
    0
    • P Pavlex4

      I created android application to stream online radio stations but it doesn't work.I want to read ip address from url and assign it to string and when user selects radio to listen I should add port to that string!!!

      public class BackgroundService extends Service implements OnCompletionListener
      {
      MediaPlayer mediaPlayer;
      private String STREAM_URL;

      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
      
      @Override
      public void onCreate()
      {
          final StringBuilder text = new StringBuilder();
      
          new Thread(new Runnable()
          {
              public void run()
              {
                  try
                  {
                      URL url = new URL("http://audiophileradio.stream/Ip.txt");
      
                      HttpURLConnection conn=(HttpURLConnection) url.openConnection();
      
                      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                      String str;
                      while ((str = in.readLine()) != null)
                      {
                          text.append(str);                      
                      }
                      in.close();
                      text.insert(text.length(), ":8000");
                  }
                  catch (Exception e)
                  {
                      Log.d("MyTag",e.toString());
                  }
              }
          }).start();
      

      SharedPreferences sharedPreferences =
      PreferenceManager.getDefaultSharedPreferences(this);
      String radio = sharedPreferences.getString("station", "8000");

          if (radio != null && radio.equals("8000"))
          {
              text.append(":8000");
              STREAM\_URL = text.toString();
              Toast.makeText(this,STREAM\_URL,Toast.LENGTH\_SHORT).show();
          }
          if (radio != null && radio.equals("8010"))
          {
              text.append(":8010");
              STREAM\_URL = text.toString();
          }
          if (radio != null && radio.equals("8020"))
          {
              text.append(":8020");
              STREAM\_URL = text.toString();
          }
          if (radio != null && radio.equals("8030"))
          {
              text.append(":8030");
              STREAM\_URL = text.toString();
          }
      
          mediaPlayer = new MediaPlayer();
          try
          {
              mediaPlayer.setDataSource(STREAM\_URL);
          } catch (IOException e)
          {
              e.printStackTrace();
          }
      
          mediaPlayer.setOnCo
      
      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Depending on how long it takes your thread code to finish parsing http://audiophileradio.stream/Ip.txt, your preferences/append code is also accessing the text variable. What's the possibility of that happening out of order?

      Pavlex4 wrote:

      mediaPlayer.setDataSource(STREAM_URL);

      Have you tried hard-coding the value passed to setDataSource() to see if the reset of your player-related code is correct? This is unrelated to the problem you are having, but the four if() tests are not as efficient as they could be. You are checking radio's value four more times than is necessary. Consider:

      if (radio != null)
      {
      text.append(':').append(radio);

      STREAM\_URL = text.toString();
      Toast.makeText(this, STREAM\_URL, Toast.LENGTH\_SHORT).show();
      

      }

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups