How to send a “multipart/form-data” POST in Android with Volley

后端 未结 9 2179
不思量自难忘°
不思量自难忘° 2020-11-22 03:50

Has anyone been able to accomplish sending a multipart/form-data POST in Android with Volley yet? I have had no success trying to upload an image/png

9条回答
  •  情歌与酒
    2020-11-22 04:19

    Here is Simple Solution And Complete Example for Uploading File Using Volley Android

    1) Gradle Import

    compile 'dev.dworks.libs:volleyplus:+'
    

    2)Now Create a Class RequestManager

    public class RequestManager {
        private static RequestManager mRequestManager;
        /**
         * Queue which Manages the Network Requests :-)
         */
        private static RequestQueue mRequestQueue;
        // ImageLoader Instance
    
        private RequestManager() {
    
        }
    
        public static RequestManager get(Context context) {
    
            if (mRequestManager == null)
                mRequestManager = new RequestManager();
    
            return mRequestManager;
        }
    
        /**
         * @param context application context
         */
        public static RequestQueue getnstance(Context context) {
    
            if (mRequestQueue == null) {
                mRequestQueue = Volley.newRequestQueue(context);
            }
    
            return mRequestQueue;
    
        }
    
    
    }
    

    3)Now Create a Class to handle Request for uploading File WebService

    public class WebService {
        private RequestQueue mRequestQueue;
        private static WebService apiRequests = null;
    
        public static WebService getInstance() {
            if (apiRequests == null) {
                apiRequests = new WebService();
                return apiRequests;
            }
            return apiRequests;
        }
        public void updateProfile(Context context, String doc_name, String doc_type, String appliance_id, File file, Response.Listener listener, Response.ErrorListener errorListener) {
            SimpleMultiPartRequest request = new SimpleMultiPartRequest(Request.Method.POST, "YOUR URL HERE", listener, errorListener);
    //        request.setParams(data);
            mRequestQueue = RequestManager.getnstance(context);
            request.addMultipartParam("token", "text", "tdfysghfhsdfh");
            request.addMultipartParam("parameter_1", "text", doc_name);
            request.addMultipartParam("dparameter_2", "text", doc_type);
            request.addMultipartParam("parameter_3", "text", appliance_id);
                request.addFile("document_file", file.getPath());
    
            request.setFixedStreamingMode(true);
            mRequestQueue.add(request);
        }
    }
    

    4) And Now Call The method Like This to Hit the service

    public class Main2Activity extends AppCompatActivity implements Response.ErrorListener, Response.Listener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            Button button=(Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    uploadData();
                }
            });
        }
    
        private void uploadData() {
            WebService.getInstance().updateProfile(getActivity(), "appl_doc", "appliance", "1", mChoosenFile, this, this);
        }
    
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    
        @Override
        public void onResponse(String response) {
         //Your response here 
        }
    }
    

提交回复
热议问题