【问题标题】:Sending multiple images using retrofit in a single key在单个密钥中使用改造发送多个图像
【发布时间】:2020-07-29 07:14:45
【问题描述】:

我已经尝试了互联网上所有可用的方法,但没有任何方法适合我。 我正在尝试以单键值发送图像

{
  files[]
}

这是我的界面

public interface UserClient {
    @Multipart
    @POST(".")
    Call<JsonElement> upload(
            @Part("text") RequestBody text,
            @Part("image") RequestBody image,
            @Part("login") RequestBody login,
            @Part List<MultipartBody.Part> files
    );
}

这是我的上传方法

public void uploadFiles(String text) {
    QBUser user = SharedPrefsHelper.getInstance().getQbUser();
    String userName = user.getFullName();
    String image = "https://image.flaticon.com/icons/svg/146/146031.svg";
    String URL = URLs.URL_POST + user.getPhone() + "/";

    RequestBody textPart = RequestBody.create(MultipartBody.FORM, text);
    RequestBody imagePart = RequestBody.create(MultipartBody.FORM, image);
    RequestBody loginPart = RequestBody.create(MultipartBody.FORM, userName);

    //list of files
    List<MultipartBody.Part> filesList = new ArrayList<>();
    for (int i = 0; i < arrayList_FilePath.size(); i++) {
        filesList.add(prepareFilePart("files" + i, arrayList_FilePath.get(i)));
    }
    //Create retrofit instance
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl(URL)
            .addConverterFactory(GsonConverterFactory.create());
    Retrofit retrofit = builder.build();

    UserClient client = retrofit.create(UserClient.class);

    Log.i(TAG, "uploadFiles: " + filesList);
    Call<JsonElement> call = client.upload(textPart, imagePart, loginPart, filesList);

    call.enqueue(new Callback<JsonElement>() {
        @Override
        public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
            finish();
            Toast.makeText(getApplicationContext(), "Successful: " + response.message(), 
            Toast.LENGTH_LONG).show();
            Log.i(TAG, "onResponse: " + response.body() + response.message());
        }

        @Override
        public void onFailure(Call<JsonElement> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_LONG).show();
        }
    });
}

这是 preparefilePart 方法

public MultipartBody.Part prepareFilePart(String partName, String path){
    long imagename = System.currentTimeMillis();
    File file = new File(path);
    RequestBody requestBody = RequestBody.create(
            MediaType.parse("image/*"),
            file
    );
    return MultipartBody.Part.createFormData(partName, imagename + ".jpeg" ,requestBody);
}

如果我只发送没有图像的文本请求,它工作正常,但是当我发送图像时它不工作 它给出了一个错误代码 500 Internal Server Error with null response in body

注意:Api 在 Postman 中运行良好

【问题讨论】:

  • @SilverskyTechnology 谢谢,但我已经尝试过这个答案,它对我不起作用
  • 我收到内部服务器错误
  • 那么我认为发送多个文件时 php 文件中的问题 php 将无法正确处理该对象
  • @SilverskyTechnology 感谢您的回复,我在下面找到了我的问题的答案

标签: java android file-upload retrofit2


【解决方案1】:

文件未上传,因为文件未正确创建 感谢 github,我找到了一种正确创建文件的方法

GitHub link for creating file

还根据此答案对我的代码进行了一些更改 link for stackoverflow answer

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2020-12-17
    • 2020-11-20
    • 2019-06-17
    • 2017-10-01
    相关资源
    最近更新 更多