首页 > 解决方案 > 使用改造从 android 应用程序使用 django restful api 将图像上传到服务器

问题描述

我一直无法使用 django api 将图像从使用改造的 android 应用程序上传到服务器。当我尝试从 android 应用程序发送图像时,不会显示任何错误,它也不会访问服务器,但使用邮递员它工作得很好。背后的原因可能是什么?我的安卓代码:

File file = new File(filePath);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
                MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
                RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "upload_test");
                retrofit2.Call<okhttp3.ResponseBody> call=RetrofitClient.getInstance().getApi().upp1(body,name);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        System.out.println(response.body());
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Toast.makeText(UploadPage.this, "Failure", Toast.LENGTH_SHORT).show();
                    }
                });

和api方法:

@Multipart @POST("testing/") 调用 upp1(@Part MultipartBody.Part image, @Part("name") RequestBody name);

每当我尝试仅发送文件名时,它都会访问服务器,但不会发送图像文件本身。

并且用于测试 django 代码是

@api_view(['POST']) def testing(request): print(request.data) return Response("Success")

标签: androiddjango

解决方案


推荐阅读