为什么无法使用多部分上传上传图片?

huangapple 未分类评论42阅读模式
英文:

why cant upload image using multipart?

问题

我在使用多部分Multipart上传图像到服务器时遇到问题当将图像上传到服务器时响应显示成功但响应内容为空且未上传任何内容但当我尝试发送不使用多部分的数据如发送电子邮件或名称数据发送成功但在同一个 API 中当我尝试使用多部分发送任何内容时服务器未收到任何内容

@Multipart
@Headers({
        "Content-Type: application/json",
        "X-Requested-With: XMLHttpRequest"})
@POST("profile")
Call<ResponseBody> upload(
        @Header("Authorization") String authorization,
        @Part MultipartBody.Part image
);

void data_1(File file) {

    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

    Retrofit retrofits = new Retrofit.Builder().baseUrl(Base_url).
            addConverterFactory(GsonConverterFactory.create()).build();
    networkis networkis = retrofits.create(www.gift_vouchers.com.NetworkLayer.networkis.class);

    Call<ResponseBody> call = networkis.upload("Bearer " + new saved_data().get_token(UserInfoModelViewFactory.context),
            body);

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            new utils().dismiss_dialog(UserInfoModelViewFactory.context);
            Log.e("response_is", "" + response.message() + "dcd" + response.isSuccessful());
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            new utils().dismiss_dialog(UserInfoModelViewFactory.context);
            Log.e("error", t.getMessage());
        }
    });
}
英文:

i have problem with uploading image to server using multipart when uploading the image to sever the response is success but it get with null response and nothing uploaded when i trying to send data without using multipart send email or name data sent successfully but when i try to sent anything using part nothing sent to the server in the same api

  @Multipart
    @Headers({
            &quot;Content-Type: application/json&quot;,
            &quot;X-Requested-With: XMLHttpRequest&quot;})
    @POST(&quot;profile&quot;)
    Call&lt;ResponseBody&gt; upload(
            @Header(&quot;Authorization&quot;) String authorization,
            @Part MultipartBody.Part image

    );


  void data_1(File file) {

        RequestBody requestFile = RequestBody.create(MediaType.parse(&quot;multipart/form-data&quot;), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData(&quot;picture&quot;, file.getName(), requestFile);

        Retrofit retrofits = new Retrofit.Builder().baseUrl(Base_url).
                addConverterFactory(GsonConverterFactory.create()).build();
        networkis networkis = retrofits.create(www.gift_vouchers.com.NetworkLayer.networkis.class);


        Call&lt;ResponseBody&gt; call = networkis.upload(&quot;Bearer &quot; + new saved_data().get_token(UserInfoModelViewFactory.context)
                , body);

        call.enqueue(new Callback&lt;ResponseBody&gt;() {
            @Override
            public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {
                new utils().dismiss_dialog(UserInfoModelViewFactory.context);
                Log.e(&quot;response_is&quot;, &quot;&quot; + response.message() + &quot;dcd&quot; + response.isSuccessful());
            }

            @Override
            public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {
                new utils().dismiss_dialog(UserInfoModelViewFactory.context);
                Log.e(&quot;error&quot;, t.getMessage());

            }
        });
    }

huangapple
  • 本文由 发表于 2020年7月27日 00:56:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/63103130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定