Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import okhttp3.MediaType;
- import okhttp3.MultipartBody;
- import okhttp3.RequestBody;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- import sun.rmi.runtime.Log;
- import java.io.File;
- public class Main {
- public static void main(String[] args) {
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://localhost:4567/")
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- ServerService api = retrofit.create(ServerService.class);
- File file = new File("/home/maxim/PicotoServer/content.jpg");
- MultipartBody.Part filePart = MultipartBody.Part.
- createFormData("file", file.getName(),
- RequestBody.create(MediaType.parse("image/*"), file));
- Call<String> call = api.upload(filePart);
- call.enqueue(new Callback<String>() {
- @Override
- public void onResponse(Call<String> call, Response<String> response) {
- if (response.body() != null) {
- System.out.println(response.body());
- }
- }
- @Override
- public void onFailure(Call<String> call, Throwable t) {
- System.out.println(t.getMessage());
- System.out.println("Failed");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement