0

I am trying to send an http post request with raw data .

may be its a duplicate question.. nut i've tried a lot but didn't get any exact solution.. May be there is some minor mistake that i'm not able to understand..

The raw data format is described below

{result_data: [project,circuit]}

what I'm doing:

public void MakeStrRawRequest(final String Tag, String url, final String appData, final ResponseListener responseListener) {

    //String uri = String.format(Locale.US, URL);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "String Success :" + response);
                }
            },
            new com.android.volley.Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "String  Error In Request :" + error.toString());

                    NetworkResponse response = error.networkResponse;
                    if (error instanceof ServerError && response != null) {
                        try {
                            String res = new String(response.data,
                                    HttpHeaderParser.parseCharset(response.headers, "utf-8"));
                            // Now you can use any deserializer to make sense of data
                            //JSONObject obj = new JSONObject(res);
                            Logger.e(res);
                        } catch (UnsupportedEncodingException e1) {
                            // Couldn't properly decode data to string
                            e1.printStackTrace();
                        }
                    }
                }
            }) {
        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            return super.parseNetworkResponse(response);
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("result_data", "[project,circuit]");
            // {result_data: [project,circuit]}
            return hashMap;
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            return appData.getBytes();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            String AuthToken = "auto_token_value";
            headers.put(ApiConstant.TOKEN_KEY, AuthToken);
            return headers;
        }
    };
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(15000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    ApplicationData.getInstance().getRequestQueue().add(stringRequest);
}

Here is the response that i'm getting..

BasicNetwork.performRequest: Unexpected response code 400 

I've tried both method to send data 1.in getParam() and 2. in getBody()

  1.   @Override 
    protected Map<String, String> getParams() throws AuthFailureError {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("result_data", "[project,circuit]");
        // {result_data: [project,circuit]} 
        return hashMap;
    } 

  2.
    @Override 
    public byte[] getBody() throws AuthFailureError {
        return appData.getBytes();
    } 
Singh
  • 43
  • 8

1 Answers1

1

getBody and getParams both are use to send parameters .So, You should call only one method at a time .If you want to send an arbitary string then use getBody() method and on the otherside if you want to send normal parameters then you should use getBody() method.For more detail you may see here

PRIYA PARASHAR
  • 777
  • 4
  • 15