做Back Back AsyncTask

时间:2013-01-22 05:14:54

标签: asynchronous exception-handling media-player runtime-error

我正在尝试为Mediaplayer加载动态网址。如何处理各种异常,如文件未找到&超时执行期间出现的异常。

public class MediaAsynchTask extends AsyncTask<String, Void, Object> {
protected void onPreExecute() {
super.onPreExecute();
progress = new ProgressDialog(AudioView.this);
progress.setMessage("LOADING........");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setCancelable(true);
progress.show();
}
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if(result!=null){
progress.dismiss();
}else{
Audio.this.finish();
}
}
Protected Object doInBackground(String... params) {
try {
if (!true) 
{
MediaPlayer m = MediaPlayer.create(Audio.this,Uri.parse("audiourl.mp3"));
myProgressBar.setMax(mMediaPlayer.getDuration());
}return mMediaPlayer;
}catch (Exception e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"Check Your NetWork and Data Connection",Toast.LENGTH_LONG).show();
}
});
} 
return mMediaPlayer;
}
}

1 个答案:

答案 0 :(得分:0)

非常简单,你可以这样做:

try{
 **//your code goes here**
}catch(FileNotFoundException exception){
//handle exception
}//**handle other possible exceptions**
catch(JsonParseException exception){
    exception.printStackTrace();
}catch(JsonProcessingException exception){
    exception.printStackTrace();
}