2017-09-14 1 views
0

Ich bekomme eine URL als Antwort. Ich möchte das HTML dieser URL herunterladen, damit der Benutzer es auch offline sehen kann. Es ist eine RecyclerView, in der jedes Element eine URL enthält. Wenn der Benutzer also auf die URL eines Elements klickt, sollte es auf dem externen Datenträger gespeichert werden.Download HTML-Datei von URL mit Retrofit

Unten finden Sie den Code ein:

NewsAdapter:

case R.id.save: 
         Gson gson = new GsonBuilder() 
           .setLenient() 
           .create(); 
         Retrofit retrofit = new Retrofit.Builder() 
           .baseUrl("https://www.nytimes.com/") 
           .addConverterFactory(GsonConverterFactory.create(gson)) 
           .build(); 
         Log.i("Retrofit build", "initiated"); 
         ApiInterface retrofitInterface = retrofit.create(ApiInterface.class); 

           final Call<ResponseBody> call = retrofitInterface.downloadFileWithDynamicUrlSync("2017/09/13/us/nursing-home-deaths-florida.html"); 

         Log.i("Retrofit req execute", "initiated"); 

          new AsyncTask<Void, Void, Void>() { 
           @Override 
           protected Void doInBackground(Void... voids) { 
            boolean writtenToDisk = false; 
            try { 
             writtenToDisk = writeResponseBodyToDisk(call.execute().body()); 
            } catch (IOException e) { 
             e.printStackTrace(); 
            } 
            ; 

            Log.d("success", "file download was a success? " + writtenToDisk); 
            return null; 
           } 
          }.execute(); 


         break; 

private boolean writeResponseBodyToDisk(ResponseBody body) { 
     try { 
      // todo change the file location/name according to your needs 
      File futureStudioIconFile = new File(Environment.DIRECTORY_DOWNLOADS + File.separator + "Future Studio Icon.png"); 

      InputStream inputStream = null; 
      OutputStream outputStream = null; 

      try { 
       byte[] fileReader = new byte[4096]; 

       long fileSize = body.contentLength(); 
       long fileSizeDownloaded = 0; 

       inputStream = body.byteStream(); 
       outputStream = new FileOutputStream(futureStudioIconFile); 

       while (true) { 
        int read = inputStream.read(fileReader); 

        if (read == -1) { 
         break; 
        } 

        outputStream.write(fileReader, 0, read); 

        fileSizeDownloaded += read; 

        Log.d("filedownload", "file download: " + fileSizeDownloaded + " of " + fileSize); 
       } 

       outputStream.flush(); 

       return true; 
      } catch (IOException e) { 
       return false; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 

       if (outputStream != null) { 
        outputStream.close(); 
       } 
      } 
     } catch (IOException e) { 
      return false; 
     } 
    } 

ApiInterface:

// option 2: using a dynamic URL 
@Streaming 
    @GET 
    Call<ResponseBody> downloadFileWithDynamicUrlSync(@Url String fileUrl); 

ich auch bin immer den Fehler:

Failed to invoke public com.squareup.okhttp.ResponseBody() with no args 

jemand kann mir sagen, wie zu Implementiere es richtig.

+0

"2017/09/13/us/Pflegeheim-Todesfälle-florida.html". Präfix Domian Name zum Download-Datei. –

Antwort

0

Verwenden Sie die URL mit dem Domänennamen zum Herunterladen der Datei. Entfernen Sie die Streaming-Anmerkung nicht.

Sie erhalten keinen Dateikörper, da Sie keine vollständige URL verwenden.

eine Schnittstelle erstellen, wie dies

@GET 
Call<ResponseBody> downloadFile(@Url String url); 

Dann diesen Code verwenden:

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> 
response) 
         { 
          if (response.isSuccessful()) { 
           String filePath = Utils.downloadFile(response.body(),"filename"); 

          } 
         } 



public String downloadFile(ResponseBody body, String fileName) { 
    String filePath = null; 
    try { 
     int count; 
     byte data[] = new byte[1024 * 4]; 
     InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8); 
     File storageDir = new File(Environment.getExternalStorageDirectory(), "AppName"); 
     if (!storageDir.exists()) { 
      storageDir.mkdirs(); 
     } 
     File outputFile = new File(storageDir, fileName); 
     OutputStream output = new FileOutputStream(outputFile); 
     while ((count = bis.read(data)) != -1) { 
      output.write(data, 0, count); 
     } 
     output.flush(); 
     output.close(); 
     bis.close(); 
     filePath = outputFile.getAbsolutePath(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return filePath; 
}