Android 安卓:有没有办法改变MediaPlayer的URL?
一旦我推向市场,有没有一种方法可以在不编译应用程序或部署的情况下更改URL?url将来可能会更改或指向不同的url 目前,我正在对URL进行硬编码,如下所示:Android 安卓:有没有办法改变MediaPlayer的URL?,android,media-player,Android,Media Player,一旦我推向市场,有没有一种方法可以在不编译应用程序或部署的情况下更改URL?url将来可能会更改或指向不同的url 目前,我正在对URL进行硬编码,如下所示: try { url = "http://ofertaweb.ro/android/sleepandlovemusic/" + songs_array[counter] + ".mp3"; mediaPlayer.setDataSource(url); } try { Get_Webpage obj = new Get_
try {
url = "http://ofertaweb.ro/android/sleepandlovemusic/" + songs_array[counter] + ".mp3";
mediaPlayer.setDataSource(url);
}
try {
Get_Webpage obj = new Get_Webpage("http://ofertaweb.ro/android/sleepandlovemusic/list_files.php");
directory_listings = obj.get_webpage_source();
} catch (Exception e) {
}
//Log.d("director listing", directory_listings);
songs_array = directory_listings.split(":::");
不要在项目生成时间硬编码URL,考虑编写在应用程序运行时动态解析它的代码。例如,您可以创建一个静态html页面(包含实际mp3 URL的列表),在项目构建时硬编码此静态html页面URL,每次应用程序开始运行时,查询此静态html页面以在应用程序运行时获取最新的mp3 URL。有很多其他方法可以实现这一点,只是给你一些线索,希望这有帮助。 < P>不要在项目生成时间硬编码你的URL,考虑编写在应用程序运行时动态解析它的代码。例如,您可以创建一个静态html页面(包含实际mp3 URL的列表),在项目构建时硬编码此静态html页面URL,每次应用程序开始运行时,查询此静态html页面以在应用程序运行时获取最新的mp3 URL。有很多方法可以实现这一点,只要给你一些线索,希望这能有所帮助。以下是我找到的一种阅读html文件的方法:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class Get_Webpage {
public String parsing_url = "";
public Get_Webpage(String url_2_get){
parsing_url = url_2_get;
}
public String get_webpage_source(){
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(parsing_url);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
String html = "";
InputStream in = null;
try {
in = response.getEntity().getContent();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
try {
while((line = reader.readLine()) != null)
{
str.append(line);
}
} catch (IOException e) {
}
try {
in.close();
} catch (IOException e) {
}
html = str.toString();
return html;
}
}
然后你会这样读:
try {
url = "http://ofertaweb.ro/android/sleepandlovemusic/" + songs_array[counter] + ".mp3";
mediaPlayer.setDataSource(url);
}
try {
Get_Webpage obj = new Get_Webpage("http://ofertaweb.ro/android/sleepandlovemusic/list_files.php");
directory_listings = obj.get_webpage_source();
} catch (Exception e) {
}
//Log.d("director listing", directory_listings);
songs_array = directory_listings.split(":::");
以下是我找到的读取html文件的方法:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class Get_Webpage {
public String parsing_url = "";
public Get_Webpage(String url_2_get){
parsing_url = url_2_get;
}
public String get_webpage_source(){
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(parsing_url);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
String html = "";
InputStream in = null;
try {
in = response.getEntity().getContent();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
try {
while((line = reader.readLine()) != null)
{
str.append(line);
}
} catch (IOException e) {
}
try {
in.close();
} catch (IOException e) {
}
html = str.toString();
return html;
}
}
然后你会这样读:
try {
url = "http://ofertaweb.ro/android/sleepandlovemusic/" + songs_array[counter] + ".mp3";
mediaPlayer.setDataSource(url);
}
try {
Get_Webpage obj = new Get_Webpage("http://ofertaweb.ro/android/sleepandlovemusic/list_files.php");
directory_listings = obj.get_webpage_source();
} catch (Exception e) {
}
//Log.d("director listing", directory_listings);
songs_array = directory_listings.split(":::");
您是否有示例代码或博客,我可以在此主题上阅读更多内容?有关示例代码,请参阅如何从URL获取html页面。您是否有示例代码或博客,我可以在此主题上阅读更多内容?有关示例代码,请参阅如何从URL获取html页面。