添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
   **//Call upload function from async task**
    @Override
    protected String doInBackground(String... strings) {
            if (strings[0] != null) {
                try {
                    file1 = new File(strings[0]);
                    RequestBody requestFile =
                            RequestBody.create(
                                    MediaType.parse(getContentResolver().getType(uri)),
                                    file1
                    // MultipartBody.Part is used to send also the actual file name
                    MultipartBody.Part body =
                            MultipartBody.Part.createFormData("file_url", file1.getName(), requestFile);
                    uploadFile(body);
                } catch (Exception e) {
                    e.printStackTrace();
            } else {
                try {
                    file2 = new File(strings[1]);
                    // create RequestBody instance from file
                    RequestBody requestFile =
                            RequestBody.create(
                                    MediaType.parse(getContentResolver().getType(uri)),
                                    file2
                    // MultipartBody.Part is used to send also the actual file name
                    MultipartBody.Part body =
                            MultipartBody.Part.createFormData("file_url", file2.getName(), requestFile);
                    uploadFile(body);
                } catch (Exception e) {
                    e.printStackTrace();
            return null;

//Retrofit Call

private void uploadFile(MultipartBody.Part body){
        Log.v("HitMan",body.toString());
        String doctypeString = "Lead";
        String docnameString = name;
        RequestBody doctype =
                RequestBody.create(
                        okhttp3.MultipartBody.FORM, doctypeString);
 RequestBody docname =
                RequestBody.create(
                        okhttp3.MultipartBody.FORM, docnameString);
        Log.v("Body",body.toString());
        Log.v("Doctype",doctype.toString());
        Log.v("Docname",docname.toString());
        UserService uploadService = retrofit.create(UserService.class);
        Call<ResponseBody> call4 = uploadService.uploadFile(body,doctype,docname);
        call4.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.isSuccessful()) {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(LeadDetailActivity.this,"Uploaded Successfully",Toast.LENGTH_SHORT).show();
                else{
                    progressBar.setVisibility(View.GONE);
                    Log.v("Custom Error :",response.message());
                    Toast.makeText(LeadDetailActivity.this,"Process Failed!",Toast.LENGTH_SHORT).show();
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                Log.v("Create Response Error", t.toString());
    @Multipart
    @POST("api/method/upload_file")
    Call<ResponseBody> uploadFile(@Part MultipartBody.Part file_url,@Part("doctype") RequestBody doctype,@Part("docname") RequestBody docname);

//Response Error Log

V/URI: content://com.mi.android.globalFileexplorer.myprovider/external_files/temp.jpg
D/File Path :: /storage/emulated/0/temp.jpg
V/FA: Activity resumed, time: 5674872
V/FA: Connecting to remote service
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 1
V/HitMan: okhttp3.MultipartBody$Part@7a76c54
V/Body: okhttp3.MultipartBody$Part@7a76c54
V/Doctype: okhttp3.RequestBody$2@242e7fd
V/Docname: okhttp3.RequestBody$2@3da0cf2
V/Custom Error :: INTERNAL SERVER ERROR

500 Error Code means Server Response Error, so I think if the end point is compatible with the post format as done using Multipart of okhttp3 client?

- - [21/Jun/2023 12:34:27] “POST /api/method/upload_file HTTP/1.1” 500 -
12:34:27 web.1 | INFO:werkzeug: - - [21/Jun/2023 12:34:27] “POST /api/method/upload_file HTTP/1.1” 500 -