如何使用Volley发送表单数据(例如在Postman中)?

时间:2019-06-21 11:35:03

标签: android post android-volley postman

我已经阅读了有关此主题的其他帖子,但没有一篇对我有用。 使用getParams()完成了原始数据的发送,但是有些帖子显示使用相同的格式发送数据。

我尝试同时使用getBody()getParams()发送,但是都返回了错误。

这就是我想要做的

StringRequest stringRequest = new StringRequest(Request.Method.POST, UPDATE_PROFILE_IMAGE_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "onResponse: " + response);
                Toast.makeText(ProfileActivity.this, String.valueOf(response), Toast.LENGTH_SHORT).show();
            }
        }, Methods.errorListener) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("id", "1");
                params.put("image", encodedString);
                return params;
            }
        };
        requestQueue.add(stringRequest);

我尝试使用Postman,并且仅使用表格数据。

2 个答案:

答案 0 :(得分:0)

尝试下面的代码可能有帮助

    private void postUsingVolley() {
    String tag_json_obj = "json_obj_req";

    final ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("posting...");
    pDialog.show();

    final String mVendorId = DeviceDetails.getInstance(mContext).getVendor_id();
    String mUserId = UserModel.getInstance(mContext).getUser_id();

    final HashMap<String, String> postParams = new HashMap<String, String>();
    sendFeedbackParams.put("key1", value1);
    sendFeedbackParams.put("key2", value2);
    sendFeedbackParams.put("key3", value3);

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            ApplicationData.POST_URL, new JSONObject(postParams),
            new com.android.volley.Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    //Log.d("TAG", response.toString());
                    try {
                        //Toast.makeText(mContext, response.getString("message"), Toast.LENGTH_LONG).show();
                        Toast.makeText(mContext, "Thank you for your post", Toast.LENGTH_LONG).show();

                        if (response.getBoolean("status")) {
                            pDialog.dismiss();
                            finish();
                        }
                    } catch (JSONException e) {
                        Log.e("TAG", e.toString());
                    }
                    pDialog.dismiss();
                }
            }, new com.android.volley.Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            //VolleyLog.d("TAG", "Error: " + error.getMessage());
            pDialog.dismiss();
            if (isNetworkProblem(error)) {
                Toast.makeText(mContext, "Internet Problem", Toast.LENGTH_SHORT).show();

            }
        }
    }) {

        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return getRequestHeaders();
        }
    };

    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(8000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}

答案 1 :(得分:0)

尝试此代码,希望它对您有用

StringRequest stringRequest = new StringRequest(Request.Method.POST, UPDATE_PROFILE_IMAGE_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, "onResponse: " + response);
            Toast.makeText(ProfileActivity.this, String.valueOf(response), Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
        }
    }) 

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("id", "1");
            params.put("image", encodedString);
            return params;
        }
    };
    requestQueue.add(stringRequest);