Volley JsonObjectRequest Post request not working

后端 未结 9 1964
有刺的猬
有刺的猬 2020-11-22 06:25

I am using android Volley for making a request. So I use this code. I don\'t understand one thing. I check in my server that params is always null. I consider that getParams

9条回答
  •  借酒劲吻你
    2020-11-22 07:18

    It worked for can try this for calling with Volley Json Request and Response ith Java Code .

    public void callLogin(String sMethodToCall, String sUserId, String sPass) {
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                    Request.Method.POST, ConstantValues.ROOT_URL_LOCAL + sMethodToCall.toString().trim(), addJsonParams(sUserId, sPass),
    //                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, object,
                    new Response.Listener() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d("onResponse", response.toString());
                            Toast.makeText(VolleyMethods.this, response.toString(), Toast.LENGTH_LONG).show(); // Test
    
                            parseResponse(response);
    //                        msgResponse.setText(response.toString());
    //                        hideProgressDialog();
                        }
                    },
                    new Response.ErrorListener() {
    
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d("onErrorResponse", "Error: " + error.getMessage());
                            Toast.makeText(VolleyMethods.this, error.toString(), Toast.LENGTH_LONG).show();
    //                hideProgressDialog();
                        }
                    }) {
    
                /**
                 * Passing some request headers
                 */
                @Override
                public Map getHeaders() throws AuthFailureError {
                    HashMap headers = new HashMap();
                    headers.put("Content-Type", "application/json; charset=utf-8");
                    return headers;
                }
    
    
            };
    
            requestQueue.add(jsonObjectRequest);
        }
    
        public JSONObject addJsonParams(String sUserId, String sPass) {
            JSONObject jsonobject = new JSONObject();
            try {
    //            {"id":,"login":"secretary","password":"password"}
    
                ///***//
                Log.d("addJsonParams", "addJsonParams");
    
    //            JSONObject jsonobject = new JSONObject();
    
    //            JSONObject jsonobject_one = new JSONObject();
    //
    //            jsonobject_one.put("type", "event_and_offer");
    //            jsonobject_one.put("devicetype", "I");
    //
    //            JSONObject jsonobject_TWO = new JSONObject();
    //            jsonobject_TWO.put("value", "event");
    //            JSONObject jsonobject = new JSONObject();
    //
    //            jsonobject.put("requestinfo", jsonobject_TWO);
    //            jsonobject.put("request", jsonobject_one);
    
                jsonobject.put("id", "");
                jsonobject.put("login", sUserId); // sUserId
                jsonobject.put("password", sPass); // sPass
    
    
    //            js.put("data", jsonobject.toString());
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
            return jsonobject;
        }
    
        public void parseResponse(JSONObject response) {
    
            Boolean bIsSuccess = false; // Write according to your logic this is demo.
            try {
                JSONObject jObject = new JSONObject(String.valueOf(response));
                bIsSuccess = jObject.getBoolean("success");
    
    
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(VolleyMethods.this, "" + e.toString(), Toast.LENGTH_LONG).show(); // Test
            }
    
        }
    

提交回复
热议问题