2017-02-28 60 views
0

正如你可以看到上面的網址下載視頻,我已經給了視頻的鏈接在靜態方式RetrofitInterface類下載,如下圖所示:傳遞字符串使用改造

@GET("/videos/toystory.mp4") 

但是,什麼如果我要通過視頻鏈接dynamically,對於例如該字符串中包含的鏈接:

String strVidLink = ".....Vid Link....."; 

使用改造的lib從URL下載的視頻,像這樣:

RetrofitInterface.class:

public interface RetrofitInterface { 

    @GET("/videos/toystory.mp4") 
    @Streaming 
    Call<ResponseBody> downloadFile(); 
} 

DownloadService.java:

private void initDownload(){ 

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("http://www.html5videoplayer.net") 
       .build(); 

     RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class); 

     Call<ResponseBody> request = retrofitInterface.downloadFile(); 
     try { 

      downloadFile(request.execute().body()); 

     } catch (IOException e) { 

      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 

     } 
    } 
+0

你可以發佈你的downloadFile方法嗎? –

回答

0

我認爲你必須動態傳遞鏈接如下圖所示。

public interface RetrofitInterface { 

    @GET("/videos/{url}") 
    @Streaming 
    Call<ResponseBody> downloadFile(@Path("url") String url); 
} 

或調用您的界面,如下所示。

Call<ResponseBody> request = retrofitInterface.downloadFile(/*Your Video url*/);