2012-02-19 23 views
1

有沒有一種方法可以改變網址,而無需編譯應用程序或部署,一旦我推向市場?該網址可能會在將來發生變化或指向不同的網址。Android:有沒有更改MediaPlayer網址的方法?

目前我硬編碼的URL somethign這樣的:

try { 
url = "http://ofertaweb.ro/android/sleepandlovemusic/" + songs_array[counter] + ".mp3"; 
mediaPlayer.setDataSource(url); 
} 

回答

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(":::"); 
4

不要在項目生成時硬編碼您的網址,可以考慮寫代碼,在應用程序運行時動態地解決它。例如,您可以創建一個靜態html頁面(包含一個實際的mp3 URL列表),在項目構建時對此靜態html頁面URL進行硬編碼,每當您的應用程序開始運行時,查詢此靜態html頁面以獲取最新的HTML頁面,日期MP3 URL在應用程序運行時。有很多替代方法來實現這一點,只是給你一些線索,希望這有助於。

+0

你有樣本代碼或博客,我可以瞭解更多關於此話題? – 2012-02-19 04:20:58

+0

請參閱[此SO問題](http://stackoverflow.com/questions/2423498/how-to-get-the-html-source-of-a-page-from-a-html-link-in-android)示例代碼如何從URL獲取html頁面。 – yorkw 2012-02-19 04:39:01

+0

1+ ..謝謝,我發佈了我的解決方案下面 – 2012-02-23 02:23:03

相關問題