简体   繁体   English

android retrofit 2 上传图片到服务器

[英]android retrofit 2 upload image to server

enter image description here在此处输入图像描述

I'm new the Android developer, I'm working on an application, I have activity is users can upload their items to the server (the text and image), but the problem is upload text is easy, I used Volley to upload the text to server, but I struggle long time to upload the multiple the images to server.我是 Android 开发人员的新手,我正在开发一个应用程序,我的活动是用户可以将他们的项目上传到服务器(文本和图像),但问题是上传文本很容易,我使用 Volley 上传文本到服务器,但我花了很长时间才将多个图像上传到服务器。 I saw the Retrofit 2 really good for multiple file upload but I have A lot problems.我看到 Retrofit 2 非常适合多文件上传,但我有很多问题。 I tried a lot but to no avail and I tried to share one of the codes with you so you can help me.我尝试了很多但无济于事,我试图与您分享其中一个代码,以便您可以帮助我。

I also use bearer tokens but I don't know it is correct or not.我也使用不记名令牌,但我不知道它是否正确。

  File file1 = new File(patch_img1);
                File file2 = new File(patch_img2);
                File file3 = new File(patch_img3);

                RequestBody image1 = RequestBody.create(MediaType.parse("image/*"),
                        file1);
                RequestBody image2 = RequestBody.create(MediaType.parse("image/*"),
                        file2);
                RequestBody image3 = RequestBody.create(MediaType.parse("image/*"),
                        file3);
                RequestBody invoice = RequestBody.create(MediaType.parse("text/plain"), invoices);
                UserSharedPref sharedPref = new UserSharedPref(CompletedServiceActivity.this);

                 WebServicesAPI request = APIClient.getApiClient("https://text.com/api/").create(WebServicesAPI.class);
                Call<ResponseBody> call = request.upload(sharedPref.getUserToken(),image1,image2,image3,invoice);


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

                    }

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

                    }
                });

my interface.class我的界面.class

@POST("invoices")
Call<ResponseBody> upload(
        @Header("Authorization") String authorization,
        @Part("image1")  RequestBody image1,
        @Part("image2")  RequestBody image2,
        @Part("image3")  RequestBody image3,
         @Part("invoice") RequestBody invoice

);

APIClient.class APIClient.class

public class APIClient {

    public static Retrofit retrofit = null;

    public static Retrofit getApiClient(String url) {


        if (retrofit == null) {
            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
            retrofit = new Retrofit.Builder().baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }

}

Try this it's work with me:试试这个它对我有用:

String path3 = image3.getPath();
File file3 = new File(path3);
// Parsing any Media type file
RequestBody requestBody3 = RequestBody.create(MediaType.parse("*/*"), file3);
MultipartBody.Part fileToUpload3 = MultipartBody.Part.createFormData("filename", file3.getName(), requestBody3);
RequestBody filename3 = RequestBody.create(MediaType.parse("text/plain"), image3.getName());

Calling method:调用方法:

UploadImage3(fileToUpload3,filename3);

Interface:界面:

@POST("uploadimage3.php")
@Multipart()
Observable<ApiResponse>UploadImage3(@Part MultipartBody.Part file, @Part("filename") RequestBody name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM