Volley getting an error response

2020-04-12 09:11发布

问题:

I am sending some post data using volley. Here is my code . It's a snippet from background service. Let me know if you want other files also. I've added the internet permission.

    public void onLocationChanged(final Location location) {
        mCurrentLocation = location;
        pref.setLocation(location);

        String url = getResources().getString(R.string.hostname); // it's a normal http 

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getApplicationContext(), "Location sent", Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        String errorMessage = "Err - data:"+error.getMessage();
                        Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT).show();
                }
              })
                    {
                        @Override
                        protected Map<String,String> getParams(){
                            Map<String,String> params = new HashMap<String, String>();
                            params.put(KEY_USERID,pref.getUserId());
                            params.put(KEY_LAT,String.valueOf(location.getLatitude()));
                            params.put(KEY_LNG,String.valueOf(location.getLongitude()));
                            return params;
                        }

                    };
        queue.add(stringRequest);
    }

回答1:

Actually i saw that the one of the param UserId was having the Null value, That was creating the problem