有没有办法在我推向市场后更改网址而不编译应用或部署?网址可能会在将来发生变化或指向不同的网址。
目前我正在对这些网址进行硬编码:
try {
url = "http://ofertaweb.ro/android/sleepandlovemusic/" + songs_array[counter] + ".mp3";
mediaPlayer.setDataSource(url);
}
答案 0 :(得分:4)
不要在项目构建时硬编码您的URL,考虑编写在应用程序运行时动态解析它的代码。例如,你可以创建一个静态html页面(包含一个实际的mp3 URL列表),在项目构建时硬编码这个静态html页面URL,每次你的应用程序开始运行时,查询这个静态html页面以获得最新的应用程序运行时的日期mp3 URL。有很多替代方法可以实现这一点,只是给你一些线索,希望这有帮助。
答案 1 :(得分:0)
这是我找到了一种阅读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 {
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(":::");