java - How to copy contents of test file which is on server into an array -
i done music player. able read file contains song_name , url on server dont know how store these info array song_name[] , song_url[] respectively. complete code , link inside code.
package com.hiphop.streamingmediaplayer;i import java.io.ioexception; import java.net.url; import java.util.scanner; import android.app.activity; import android.media.audiomanager; import android.media.mediaplayer; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.button; public class jsonmedia extends activity { private mediaplayer mp; button play; string song_url[], song_name[]; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.jsonview); try { url url = new url("http://reallifethug.webs.com/temp_list.txt"); scanner s = new scanner(url.openstream()); while (s.hasnextline()) { song_url = s.nextline(); } s.close(); } catch (ioexception ex) { // there connection problem, or file did not exist on // server, // or url not in right format. // think now, , put here. ex.printstacktrace(); // now, output it. } mp = new mediaplayer(); mp.setaudiostreamtype(audiomanager.stream_music); play = (button) findviewbyid(r.id.play); play.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub try { mp.setdatasource(song_url); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { mp.prepare(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } mp.start(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.jsonmedia, menu); return true; } }
you can use list , push new elements it
private string list<string> song_url; private string list<string> song_name; //inside oncreate method song_url = new arraylist<>(); song_name = new arraylist<>(); while (s.hasnextline()) { //instead of //song_url = s.nextline(); string newurl = s.nextline(); song_url.add(newurl); } but please note going correctly save new song url if song names listed in every new lines.
Comments
Post a Comment