2015-10-01 7 views
9

Ich muss alle Arten von Datei (Binär, Bild, Text, etc.) mit Retrofit-Bibliothek in meiner App herunterladen. Alle Beispiele im Netz verwenden HTML-GET-Methode. Ich muss POST verwenden, um automatisches Caching zu verhindern.Wie Datei in Android mit Retrofit-Bibliothek herunterladen?

Meine Frage ist, wie man eine Datei mit POST-Methode in Retrofit herunterladen?

+0

try this: http://stackoverflow.com/questions/32878478/how-to-download-file-in -android-using-retrofit-library/36682027 # 36682027 –

Antwort

7

Verwenden @Streaming

Asynchronous

EDIT 1

//On your api interface 
@POST("path/to/your/resource") 
@Streaming 
void apiRequest(Callback<POJO> callback); 

restAdapter.apiRequest(new Callback<POJO>() { 
     @Override 
     public void success(POJO pojo, Response response) { 
      try { 
       //you can now get your file in the InputStream 
       InputStream is = response.getBody().in(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void failure(RetrofitError error) { 

     } 
    }); 

Synchrone

//On your api interface 
@POST("path/to/your/resource") 
@Streaming 
Response apiRequest(); 

Response response = restAdapter.apiRequest(); 

try { 
    //you can now get your file in the InputStream 
    InputStream is = response.getBody().in(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

Danke für die Antwort, ich werde es versuchen und werde Ihnen antworten. – Ehsan

+0

kann Symbol POJO nicht auflösen. Wie löst man das? – Ehsan

+0

Das ist eine Objektklasse, Sie können sie nur in 'Object' ändern. – NaviRamyle

2

Wenn Sie Retrofit 2.0.0 verwenden, können Sie meine answer unter der Frage - Use retrofit to download image file verweisen.

Der entscheidende Punkt ist Verwendung okhttp3.ReponseBody, um die rohen Binärdaten zu empfangen, nicht irgendein POJO.

Und Sie wollen POST Methode verwenden, um die Datei zu erhalten, ist es einfach, nur @GET-@POST ändern, aber es abhängen, ob der Server die POST Methode unterstützen! Dies ist

1

Wie Datei 2

public interface ServerAPI { 
     @GET 
     Call<ResponseBody> downlload(@Url String fileUrl); 

     Retrofit retrofit = 
       new Retrofit.Builder() 
         .baseUrl("http://192.168.43.135/retro/") // REMEMBER TO END with/
         .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

} 

    //How To Call 
public void download(){ 
     ServerAPI api = ServerAPI.retrofit.create(ServerAPI.class); 
     api.downlload("http://192.168.43.135/retro/pic.jpg").enqueue(new Callback<ResponseBody>() { 
        @Override 
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
         try { 
          File path = Environment.getExternalStorageDirectory(); 
          File file = new File(path, "file_name.jpg"); 
          FileOutputStream fileOutputStream = new FileOutputStream(file); 
          IOUtils.write(response.body().bytes(), fileOutputStream); 
         } 
         catch (Exception ex){ 
         } 
        } 

        @Override 
        public void onFailure(Call<ResponseBody> call, Throwable t) { 
        } 
       }); 
} 
+0

Dieser Code wird nicht einmal kompiliert. Sie können Dinge nicht in einer 'Schnittstelle' initialisieren. –

+2

hast du versucht es zu kompilieren? es funktioniert für mich, und das ist, wie ich es gewohnt –

+0

Mein schlecht, du hast Recht, ich konvertierte es zu Kotlin :) –

0

Fügen Sie folgende Funktion in MainActivity.java in Retrofit DOWNLOAD:

void getRetrofitImage() { 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(url) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    RetrofitImageAPI service = retrofit.create(RetrofitImageAPI.class); 

    Call<ResponseBody> call = service.getImageDetails(); 

    call.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Response<ResponseBody> response, Retrofit retrofit) { 

      try { 

       Log.d("onResponse", "Response came from server"); 

       boolean FileDownloaded = DownloadImage(response.body()); 

       Log.d("onResponse", "Image is downloaded and saved ? " + FileDownloaded); 

      } catch (Exception e) { 
       Log.d("onResponse", "There is an error"); 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void onFailure(Throwable t) { 
      Log.d("onFailure", t.toString()); 
     } 
    }); 
} 

Datei-Handling Teil des Bild-Download wird:

private boolean DownloadImage(ResponseBody body) { 

    try { 
     Log.d("DownloadImage", "Reading and writing file"); 
     InputStream in = null; 
     FileOutputStream out = null; 

     try { 
      in = body.byteStream(); 
      out = new FileOutputStream(getExternalFilesDir(null) + File.separator + "AndroidTutorialPoint.jpg"); 
      int c; 

      while ((c = in.read()) != -1) { 
       out.write(c); 
      } 
     } 
     catch (IOException e) { 
      Log.d("DownloadImage",e.toString()); 
      return false; 
     } 
     finally { 
      if (in != null) { 
       in.close(); 
      } 
      if (out != null) { 
       out.close(); 
      } 
     } 

     int width, height; 
     ImageView image = (ImageView) findViewById(R.id.imageViewId); 
     Bitmap bMap = BitmapFactory.decodeFile(getExternalFilesDir(null) + File.separator + "AndroidTutorialPoint.jpg"); 
     width = 2*bMap.getWidth(); 
     height = 6*bMap.getHeight(); 
     Bitmap bMap2 = Bitmap.createScaledBitmap(bMap, width, height, false); 
     image.setImageBitmap(bMap2); 

     return true; 

    } catch (IOException e) { 
     Log.d("DownloadImage",e.toString()); 
     return false; 
    } 
} 

Sie können dies für vollständige Tutorial sehen: Image Download using Retrofit 2.0

0

ich die jede Art von Datei mit Retrofit zum Herunterladen des folgenden Code verwendet ...

File file = new File("Your_File_path/name"); 

    private void startDownload() { 

    if (!NetWorkUtils.getInstance(context).isNetworkAvailable()) { 
     Toast.makeText(context, "No data connection available", Toast.LENGTH_SHORT).show(); 
     return; 
    } 

    showProgressDialog(); 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(FILE_BASE_URL) 
      .build(); 

    FileHandlerService handlerService = retrofit.create(FileHandlerService.class); 

    Call<ResponseBody> call = handlerService.downloadFile(mFileName); 
    call.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      dismissProgressDialog(); 
      if (response.isSuccessful()) { 
       if (writeResponseBodyToDisk(response.body())) { 
        listener.onFileLoaded(file); 
       } 
      } else { 
       listener.onDownloadFailed("Resource not Found"); 
      } 
     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 
      dismissProgressDialog(); 
      listener.onDownloadFailed("Download Failed"); 
      t.printStackTrace(); 
     } 
    }); 

} 


interface FileHandlerService { 

    @GET("uploads/documents/{file_name}") 
    Call<ResponseBody> downloadFile(
      @Path("file_name") String imageName); 
} 

private boolean writeResponseBodyToDisk(ResponseBody body) { 
    try { 

     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(file); 

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

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

       outputStream.write(fileReader, 0, read); 

       fileSizeDownloaded += read; 

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

      outputStream.flush(); 

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

funktioniert es auf meinem Android 5.1 aber nicht auf anderen. Ich habe es in Android 6 getestet und funktioniert nicht 0 – Rodrigo