saurabh
saurabh

Reputation: 31

how to send post request with raw json using volley in java android

my sample input that i have to send in post request to the web service is

{
  "Inputs": {
    "input1": {
      "Values": [
        [
          "12-12-2012",
          "100",
          "100",
          "10",
          "10",
          "10",
          "value"
        ]
     ]
    }
  }
}

and from my sample output that is coming from service, I need only values i.e 75.7...

    {
    "Results": {
        "output1": {
            "type": "table",
            "value": {
                "ColumnNames": [
                    "Scored Label Mean"
                ],
                "ColumnTypes": [
                    "Double"
                ],
                "Values": [
                    [
                        "75.7329833331033"
                    ]
                ]
            }
        }
    }
}

I am trying to get it using volley but I don't know how to send raw json with the help of volley

my code is

JSONObject jsonObject = new JSONObject();

    try {
        JSONArray value = new JSONArray();
        value.put("12-12-2012");
        value.put("100");
        value.put("100");
        value.put("10");
        value.put("10");
        value.put("10");
        value.put("value");

        JSONArray Values = new JSONArray();
        Values.put(value);

        JSONObject Inputs = new JSONObject();
        JSONObject input1 = new JSONObject();
        input1.put("Values", Values);
        Inputs.put("input1", input1);
        jsonObject.put("Inputs", Inputs);


            Log.d("check", "fetchDataFromServer:" + jsonObject);

        } catch (JSONException e) {
            e.printStackTrace();
        }


        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.POST, URL_Helper.URL, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Toast.makeText(MainActivity.this, "response", Toast.LENGTH_SHORT).show();
                        Log.d("123", "response:" + response.toString());

                        msgResponse.setText(response.toString());
                        progressDialog.cancel();
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("123", "Error: " + error.getMessage());
                progressDialog.cancel();
                Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
                map.put("Authorization", "Bearer TFPAG2oT9sLWUK2FFwvxMm6ZKwRVBEaN7wyL5W5h3GRVgcn6K/uqRTlqRJpwgF9w==");
                map.put("Content-Type", "application/json");
                return map;
            }


        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(request);
    }

and i am getting error

E/Volley: [7113] BasicNetwork.performRequest: Unexpected response code 400 for https://ussouthcentral.services.azure....

Upvotes: 3

Views: 1284

Answers (0)

Related Questions