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

后端 未结 9 2287
不思量自难忘°
不思量自难忘° 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:38

    This is my way of doing it. It may be useful to others :

    private void updateType(){
        // Log.i(TAG,"updateType");
         StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener() {
    
             @Override
             public void onResponse(String response) {
                 // running on main thread-------
                 try {
                     JSONObject res = new JSONObject(response);
                     res.getString("result");
                     System.out.println("Response:" + res.getString("result"));
    
                     }else{
                         CustomTast ct=new CustomTast(context);
                         ct.showCustomAlert("Network/Server Disconnected",R.drawable.disconnect);
                     }
    
                 } catch (Exception e) {
                     e.printStackTrace();
    
                     //Log.e("Response", "==> " + e.getMessage());
                 }
             }
         }, new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError volleyError) {
                 // running on main thread-------
                 VolleyLog.d(TAG, "Error: " + volleyError.getMessage());
    
             }
         }) {
             protected Map getParams() {
                 HashMap hashMapParams = new HashMap();
                 hashMapParams.put("key", "value");
                 hashMapParams.put("key", "value");
                 hashMapParams.put("key", "value"));
                 hashMapParams.put("key", "value");
                 System.out.println("Hashmap:" + hashMapParams);
                 return hashMapParams;
             }
         };
         AppController.getInstance().addToRequestQueue(request);
    
     }
    

提交回复
热议问题