Volley String Request does'nt give Response in android

爷,独闯天下 提交于 2019-12-22 01:04:19

问题


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-


回答1:


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;
            }
        }



回答2:


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



来源:https://stackoverflow.com/questions/48595757/volley-string-request-doesnt-give-response-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!