我创建了一个应用程序,用于从URL收听webradio,并从JSON URL显示艺术家的名字/歌曲。它正在工作,但我想在新歌开始时更改这些名称。为此,我想我需要刷新JSON URL中的数据,但我不知道如何。
我尝试了很多教程但是找不到解决方法。谢谢。
主要活动:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btn_play = (Button) findViewById(R.id.buttonplay);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
progressDialog = new ProgressDialog(this);
if (initialStage) {
new Player().execute("http://www.radioking.com/play/tft-radio");
btn_play.setBackgroundResource(R.drawable.pause);
} else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
btn_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!playPause) {
btn_play.setBackgroundResource(R.drawable.pause);
mediaPlayer.start();
playPause = true;
} else {
btn_play.setBackgroundResource(R.drawable.playbutton);
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
playPause = false;
}
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
class Player extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
Boolean prepared = false;
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
initialStage = true;
playPause = false;
btn_play.setText("Lecture");
mediaPlayer.stop();
mediaPlayer.reset();
RestOperation test = new RestOperation();
String test2 = null;
try {
test2 = test.execute(restURL).get().toString();
Log.i("result", test2);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
splitArray = test2.split(",");
artiste = (splitArray[0]).replace("{\"artist\":\"","");
finalArtiste = artiste.replace("\"","");
titre = (splitArray[1]).replace("\"title\":\"","");
finalTitre = titre.replace("\"","");
image = (splitArray[3]).replace("\"cover\":\"","");
finalImage = image.replace("\"","");
finalImage2 = finalImage.replace("\\","");
Log.i("splitage",finalImage2);
tvTitre = (TextView) findViewById(R.id.tv_titre);
tvArtiste = (TextView) findViewById(R.id.tv_artiste);
tvTitre.setText("Titre : " + finalTitre);
tvArtiste.setText("Artiste : " + finalArtiste);
welcome = (ImageView) findViewById(R.id.imgArtiste);
if(finalImage2.contains("http") ){
Picasso.with(this).load(finalImage2).into(welcome);
}else {
welcome.setBackgroundResource(R.drawable.logokol);
}
}
});
mediaPlayer.prepare();
prepared = true;
} catch (Exception e) {
Log.e("MyAudioStreamingApp", e.getMessage());
prepared = false;
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (progressDialog.isShowing()) {
progressDialog.cancel();
}
mediaPlayer.start();
initialStage = false;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Chargement...");
progressDialog.show();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private class RestOperation extends AsyncTask<String, Void, String> {
String content;
String error;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
String data = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setTitle("Please wait ...");
// progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
BufferedReader br = null;
URL url;
try {
url = new URL(params[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter outputStreamWr = new OutputStreamWriter(connection.getOutputStream());
outputStreamWr.write(data);
outputStreamWr.flush();
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine())!=null) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
content = sb.toString();
} catch (MalformedURLException e) {
error = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
error = e.getMessage();
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return content;
}
}
}
答案 0 :(得分:0)
您必须将网络服务电话添加到CompletionListener
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
test2 = test.execute(restURL).get().toString();
}
});
答案 1 :(得分:0)
test2 = test.execute(restURL).get().toString();
不是您应该执行异步任务的方式(AsyncTask根据定义为asynchrone)。
您应该在onPostExecute(..)方法中使用您的结果。
查看https://developer.android.com/reference/android/os/AsyncTask.html