4

I am very new in android and trying to develop application to load all mp3 files from server to list-view and by clicking list item it should play that mp3 file directly from server.

Somehow i am able to list all mp3 files from server to list-view but when i click on list-view's item to play that particular song it giving me following error.

Logcat

02-18 11:49:58.266: E/AndroidRuntime(8276): FATAL EXCEPTION: main
02-18 11:49:58.266: E/AndroidRuntime(8276): java.lang.ClassCastException: java.net.URL cannot be cast to java.lang.String
02-18 11:49:58.266: E/AndroidRuntime(8276):     at iqual.fidol_final.ServerFileList$1.onItemClick(ServerFileList.java:72)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AdapterView.performItemClick(AdapterView.java:295)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AbsListView.performItemClick(AbsListView.java:1073)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2577)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AbsListView$1.run(AbsListView.java:3302)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.os.Handler.handleCallback(Handler.java:605)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.os.Looper.loop(Looper.java:154)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.app.ActivityThread.main(ActivityThread.java:4624)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
02-18 11:49:58.266: E/AndroidRuntime(8276):     at dalvik.system.NativeStart.main(Native Method)

Java Code

public class ServerFileList extends Activity implements
        OnBufferingUpdateListener, OnErrorListener, OnPreparedListener {

    Uri uri;
    URL urlAudio;
    ListView mListView;
    PlaySongAsy play;

    ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    MediaPlayer mp = new MediaPlayer();
    private List<String> myList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.serverfilelist);

        mListView = (ListView) findViewById(R.id.listAudio);

        if (Const.server == 0) {
            new getAudiofromServer().execute();
        } else {
            new getVideofromServer().execute();
        }

        // new downloadAudio().execute();

        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // playSong(urlAudio + myList.get(position));
                play = (PlaySongAsy) new PlaySongAsy(myList.get(position)
                        .replace(" ", "%20").trim()).execute();
            }
        });
    }

    class PlaySongAsy extends AsyncTask<String, Void, Boolean> {

        String baseURL;

        public PlaySongAsy(String baseURL) {
            this.baseURL = baseURL;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog = ProgressDialog.show(ServerFileList.this,
                    "    Buffering...", "please wait..", false);
            pDialog.setCancelable(false);

        }

        @Override
        protected Boolean doInBackground(String... urls) {
            new Thread() {
                @Override
                public void run() {
                    play(baseURL);
                }
            }.start();
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            // progressDialog.dismiss();
        }
    }

    private void play(String baseURL) {
        Uri myUri = Uri.parse(baseURL);
        try {
            if (mp == null) {
                this.mp = new MediaPlayer();
            } else {
                mp.stop();
                mp.reset();
            }
            mp.setDataSource(this, myUri); // Go to Initialized state

            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnPreparedListener(this);
            mp.setOnBufferingUpdateListener(this);
            mp.setOnErrorListener(this);

            mp.prepareAsync();

            // mp.setVolume(5.F, 5.F);

            Log.d("", "LoadClip Done");
        } catch (Throwable t) {
            Log.d("", t.toString());
        }
    }

    public void updateProgress(int currentSize, int totalSize) {
        TextView mTextView = new TextView(ServerFileList.this);
        mTextView.setText(Long.toString((currentSize / totalSize) * 100) + "%");
    }
    }
  • `doInbackground` is invoked on a background thread. What is the need for you start a new thread there? – Raghunandan Feb 18 '14 at 06:31
  • 1
    `playSong(urlAudio.toString() + myList.get(position));`, note the `toString` –  Feb 18 '14 at 06:32
  • @Raghunandan, what are you trying to say, sorry but i didn't get you. –  Feb 18 '14 at 06:32
  • @IK-45 why do you need to start a thread in `doInbackground`. remove `new Thread() {..` in `doInBackground`. – Raghunandan Feb 18 '14 at 06:33
  • @RC., that is commented part now i am not using that method. I am trying to implement with Asynctask. –  Feb 18 '14 at 06:34
  • @IK-45 that was the cause of the error shown in the logcat you posted.. –  Feb 18 '14 at 06:36
  • @Raghunandan, i tried it doesn't solve the problem. and this is the code i have in doinback... protected Boolean doInBackground(String... urls) { play(baseURL); return true; } –  Feb 18 '14 at 06:38
  • @IK-45 that does not solve the problem however the thread inside doInbackground is not required. my suggestion has nothing to do with your problem – Raghunandan Feb 18 '14 at 06:40

5 Answers5

2

The code:

myList.get(position)

is implicitly casting the element of myList to a String, since myList is a List<String>. This is failing because you managed to insert a URL object into myList.

Enable "unchecked cast" warnings, and you should see a warning where you are inserting into myList.

Two possible fixes:

  1. Eliminate the unchecked cast. Part of that will probably involve calling .toString() on your URL before adding it to myList.

  2. Or, change the type of myList to List<URL>, and then do the .toString() call inside onItemClick.

Which one you go with depends on the type you'd like myList to have.

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
1

I think that myList contains wrong objects. You put URIs inside it, not Strings. Use toString method on Uris when you filling list.

Find where you do this:

myList.add((String)uri);

and change to:

myList.add(uri.toString());
Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • I have used ivy apache library to list all files, so my mylist is `myList = lister1.listAll(urlAudio);` like this instead of `myList.add(uri.toString());` so how can i change this. –  Feb 18 '14 at 07:04
0

Invoke toString() method on the URL object to get the string form. Refer http://docs.oracle.com/javase/6/docs/api/java/net/URL.html#toString%28%29

Kishore
  • 819
  • 9
  • 20
0

Your `Exception saying...

java.lang.ClassCastException: java.net.URL cannot be cast to java.lang.String

Which boldly indicating, you are trying to cast an URL to a String object...that means, baseURL is an URL object but you are trying to cast it to a String object, that's why Exception happening.

Now, Change the following...

String baseURL;

to

Uri baseURL;

As well as, where you are referencing baseURL as a String, change them as Uri.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
-1

Try removing all the unnecessary spaces in your URL by calling trim(). Please refer the below question as well

Android: howto parse URL String with spaces to URI object?

Community
  • 1
  • 1
virtualpathum
  • 753
  • 2
  • 11
  • 23
  • I have already used trim() while calling asynctask, but this doesn't help. –  Feb 18 '14 at 06:41