android - 使用改造从 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")
解决方案
推荐阅读
- c# - 尝试检查列表中是否存在坐标,以便我可以忽略
- csv - Clojure - 解析小型 CSV 文件的内存使用情况
- gcc - 获取所有 fortran 浮点错误 (FPE) 警告的列表?
- sql-server - 当文本包含非日语字符时,日语中的 SQL 排序顺序会中断
- angular - 如何使用angular8根据高级按钮搜索过滤列表
- python - 这个错误是什么意思:“UnboundLocalError:分配前引用的局部变量'pos'”
- python - Python+Selenium: AssertionError: Lists different: [] != ["'This[99 chars] that\\n you give the Sto[183 chars]it."]
- python - 当需要符号表达式时,SymPy 会输出数值结果
- c# - 无法绑定到 Xamarin 中的 System.String
- kubernetes-helm - Helm - 默认使用全局值