首页 > 解决方案 > 无法从 Android rest_upload_sideload_error 上传到 wordpress

问题描述

我正在尝试使用以下路径通过 WP REST API 上传文件:/wp-json/wp/v2/media

POSTMAN 的工作正常,但是当我尝试从 android 应用程序上传时,出现此错误:

“代码”:“rest_upload_invalid_disposition”

这是上传方法:

private void SendDetail(final String token) {

        uploadMultipart();

        final ProgressDialog loading = new ProgressDialog(UploadPhoto.this);
        loading.setMessage("Please Wait...");
        loading.show();
        loading.setCanceledOnTouchOutside(false);
        DefaultRetryPolicy mRetryPolicy = new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Const.UPLOAD_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("Tsto", "onResponse:1 "+response);
                        try {
                            loading.dismiss();
                            Log.d("JSON", response);

                            JSONObject eventObject = new JSONObject(response);
                            String error_status = eventObject.getString("error");
                            if (error_status.equals("true")) {
                                String error_msg = eventObject.getString("msg");
                                ContextThemeWrapper ctw = new ContextThemeWrapper( UploadPhoto.this, R.style.Theme_AppCompat);
                                final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
                                alertDialogBuilder.setTitle("Vendor Detail");
                                alertDialogBuilder.setCancelable(false);
                                alertDialogBuilder.setMessage(error_msg);
                                alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {

                                    }
                                });
                                alertDialogBuilder.show();

                            } else {
                                String error_msg = eventObject.getString("msg");
                                ContextThemeWrapper ctw = new ContextThemeWrapper( UploadPhoto.this, R.style.Theme_AppCompat_Dialog_Alert);
                                final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
                                alertDialogBuilder.setTitle("Registration");
                                alertDialogBuilder.setCancelable(false);
                                alertDialogBuilder.setMessage(error_msg);
//                                alertDialogBuilder.setIcon(R.drawable.doubletick);
                                alertDialogBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        Intent intent=new Intent(UploadPhoto.this,LoginActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                });
                                alertDialogBuilder.show();
                            }
                        }catch(Exception e){
                            Log.d("Tag", e.getMessage());

                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        ....*** Error Handling here ... 



                            alertDialogBuilder.show();
                        }

                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<String,String>();
                map.put(KEY_User_Document1,Document_img1);
                Log.d("Network", map.toString());
                return map;
            }

            @Override       //Send Header
            public Map<String, String> getHeaders() {

                Map<String, String> params = new HashMap<>();
                //params.put("api_call_header", "header_value");
                params.put("Authorization", "Bearer " + token);

                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        stringRequest.setRetryPolicy(mRetryPolicy);
        requestQueue.add(stringRequest);
    }


这是我使用的 uploadMultipart 方法

public void uploadMultipart() {
        //getting name for the image
       // String name = editText.getText().toString().trim();

        //getting the actual path of the image
        String path = getPath(filePath);

        //Uploading code
        try {
            String uploadId = UUID.randomUUID().toString();
            // Random Name for avatars
            final String random =  GeneratePassword.randomString(7);
            //Creating a multi part request
            new MultipartUploadRequest(this, uploadId, Const.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name", random) //Adding text parameter to the request
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

所以我在这里做错了什么

标签: androidwordpresswordpress-rest-api

解决方案


推荐阅读