Advertisement
GerONSo

Untitled

Jun 2nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import okhttp3.MediaType;
  2. import okhttp3.MultipartBody;
  3. import okhttp3.RequestBody;
  4. import retrofit2.Call;
  5. import retrofit2.Callback;
  6. import retrofit2.Response;
  7. import retrofit2.Retrofit;
  8. import retrofit2.converter.gson.GsonConverterFactory;
  9. import sun.rmi.runtime.Log;
  10.  
  11. import java.io.File;
  12.  
  13. public class Main {
  14. public static void main(String[] args) {
  15. Retrofit retrofit = new Retrofit.Builder()
  16. .baseUrl("http://localhost:4567/")
  17. .addConverterFactory(GsonConverterFactory.create())
  18. .build();
  19. ServerService api = retrofit.create(ServerService.class);
  20. File file = new File("/home/maxim/PicotoServer/content.jpg");
  21. MultipartBody.Part filePart = MultipartBody.Part.
  22. createFormData("file", file.getName(),
  23. RequestBody.create(MediaType.parse("image/*"), file));
  24. Call<String> call = api.upload(filePart);
  25. call.enqueue(new Callback<String>() {
  26. @Override
  27. public void onResponse(Call<String> call, Response<String> response) {
  28. if (response.body() != null) {
  29. System.out.println(response.body());
  30. }
  31. }
  32.  
  33. @Override
  34. public void onFailure(Call<String> call, Throwable t) {
  35. System.out.println(t.getMessage());
  36. System.out.println("Failed");
  37. }
  38. });
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement