Jayashri
Jayashri

Reputation: 21

Volley String Request does'nt give Response in android

I used Volley String request for Post method..but its not giving correct response..When i used JsonObject request then it will give me correct response..I am so confused ,i don't understand what is problem with string request..Please anyone can help me with string request... this is my code..

      StringRequest requestQueue =Volley.newRequestQueue(MainActivity.this);
      String URL ="http://demoangularaims.azurewebsites.net/api/v1/API_NI_ACD_FILL_COMMON_DDL_SP";

      Log.d(TAG, " url=" + URL);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d(TAG, " response=" + response);
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d(TAG, " error=" + error);

    }
}) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {

        LinkedHashMap<String, String> linkmap = new LinkedHashMap<>();
        linkmap.put("p_ENTITY_ID", "2");
        linkmap.put("p_ORGCD", "p01");
        linkmap.put("p_COMPCD", "A0002");
        linkmap.put("p_DIVCD", "");
        linkmap.put("p_USERID", "");
        linkmap.put("p_ACDYR", "");
        linkmap.put("p_TYPE", "ACDYR_DDL");
        linkmap.put("p_FILTER1", "");
        linkmap.put("p_FILTER2", "");
        linkmap.put("p_DEFUNCT", "");

        Log.d(TAG, " MAP=" + linkmap);
        return linkmap;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        LinkedHashMap<String, String> headers = new LinkedHashMap<>();
        return headers;
    }


};

 requestQueue.add(stringRequest);

}


    PostMan OutPut-

enter image description here

Upvotes: 0

Views: 3754

Answers (2)

Ratilal Chopda
Ratilal Chopda

Reputation: 4220

Try this

JsonObjectRequest

RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
String URL = "http://demoangularaims.azurewebsites.net/api/v1/API_NI_ACD_FILL_COMMON_DDL_SP";

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("TAG", "JSONObj response=" + response);
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("TAG", "JSONObj Error: " + error.getMessage());
                //Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
            }
        });

requestQueue.add(jsonObjReq);

Jsonobject Request OUTPUT

D/TAG: JSONObj response={"recordsets":[],"output":{},"rowsAffected":[],"returnValue":0}

String Request

RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
String URL = "http://demoangularaims.azurewebsites.net/api/v1/API_NI_ACD_FILL_COMMON_DDL_SP";

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("TAG", "String response=" + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("TAG", "String error=" + error);

            }
        });
        requestQueue.add(stringRequest);

String Request OUTPUT

D/TAG: String response={"recordsets":[],"output":{},"rowsAffected":[],"returnValue":0}

POSTMAN OUTPUT

enter image description here

Upvotes: 0

Akshay
Akshay

Reputation: 318

Use getBody instead of getParams it will work. Since you are using POST method you should add Request body.

       @Override
        public byte[] getBody() throws AuthFailureError {
            try {                    
                return stringRequestBody.getBytes("utf-8"); //String Request Body with Encoded
            } catch (UnsupportedEncodingException uee) {
                return null;
            }
        }

Upvotes: 1

Related Questions