java - Android - Fill string array with urls -
i have url "yyyyyyy.com/test.txt", text file. contains urls of .mp3 audios.
yyyyyyy.de/1.mp3 yyyyyyy.de/2.mp3 yyyyyyy.de/3.mp3 //exactly
my intention read each line of text file , store in array
urls[0]=yyyyyyy.de/1.mp3 urls[1]=yyyyyyy.de/2.mp3 ...
this.
string[] urls; int i=0; random rand; int min=0; int max=5; // have 6 urls in text file int randomnum; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); rand = new random(); randomnum = rand.nextint((max - min) + 1) + min; //generates integer between 0-5 try { // create url desired page url url = new url("yyyyy.de/test.txt"); //my text file location // read text returned server bufferedreader in = new bufferedreader(new inputstreamreader(url.openstream())); string str; while ((str = in.readline()) != null) { urls[i]=str; i++; } in.close(); } catch (malformedurlexception e) { } catch (ioexception e) { } boolean isplaying = false; if (!isplaying) { isplaying = true; mediaplayer mp = new mediaplayer(); try { mp.setdatasource(urls[randomnum]); mp.prepare(); mp.start(); } catch (ioexception e) { } } else { isplaying = false; } }
i have add in manifest.xml android:permission. don't know problem is...the app closes , tells me line " mp.setdatasource(urls[randomnum]);
" wrong
here logcat : http://textuploader.com/5iw5b in advance !!
your array null
put code after super.oncreate
urls=new string[max]
update 3:
try this:
arraylist<string> urls=new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); try { // create url desired page url url = new url("yyyyy.de/test.txt"); //my text file location // read text returned server bufferedreader in = new bufferedreader(new inputstreamreader(url.openstream())); string str; while ((str = in.readline()) != null) { urls.add(str); } in.close(); } catch (malformedurlexception e) { } catch (ioexception e) { } log.i("test","url count="+urls.size()); boolean isplaying = false; if (!isplaying) { isplaying = true; mediaplayer mp = new mediaplayer(); try { int randompos = new random().nextint(urls.size()); mp.setdatasource(urls.get(randompos)); mp.prepare(); mp.start(); } catch (ioexception e) { } } else { isplaying = false; } }
Comments
Post a Comment