Volley string request error while passing string with null value as param
Asked Answered
L

2

7

I am using the following code for string request on volley

    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Loading...");
    pDialog.setCancelable(false);
    pDialog.show();
    StringRequest strReq = new StringRequest(Method.POST,
        url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                pDialog.dismiss();
                Log.e(tag, response);                   
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                pDialog.dismiss();
            }

        }) {

        protected Map<String, String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("tag", "SHOW_FILE");
            params.put("filename",filename);
            return params;
        };

    };      
    // Adding request to request queue
    Application.getInstance().addToRequestQueue(strReq, "get_file");

If the variable filename=null, I am getting the following error:

08-02 10:28:06.192: E/Volley(2935): [2128] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
08-02 10:28:06.192: E/Volley(2935): java.lang.NullPointerException
08-02 10:28:06.192: E/Volley(2935):     at libcore.net.UriCodec.encode(UriCodec.java:132)
08-02 10:28:06.192: E/Volley(2935):     at java.net.URLEncoder.encode(URLEncoder.java:57)
08-02 10:28:06.192: E/Volley(2935):     at com.android.volley.Request.encodeParameters(Request.java:449)
08-02 10:28:06.192: E/Volley(2935):     at com.android.volley.Request.getBody(Request.java:435)
08-02 10:28:06.192: E/Volley(2935):     at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)
08-02 10:28:06.192: E/Volley(2935):     at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
08-02 10:28:06.192: E/Volley(2935):     at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
08-02 10:28:06.192: E/Volley(2935):     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
08-02 10:28:06.192: E/Volley(2935):     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)

Is there need a null value check on all param I POST or any other methods to solve this problem and handle with NULL value ?

Laevorotation answered 2/8, 2014 at 5:14 Comment(0)
L
21

Thanks Giru Bhai for your help

I have created a new method to solve this problem while variable become null

       protected Map<String, String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("key1", value1);
            params.put("key2",value2);
            params.put("key3",value3);
            return checkParams(params);
        };


        private Map<String, String> checkParams(Map<String, String> map){               
            Iterator<Entry<String, String>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> pairs = (Map.Entry<String, String>)it.next();
                if(pairs.getValue()==null){
                    map.put(pairs.getKey(), "");
                }
            }
            return map;             
        }

I think this is more easy than checking each values before putting it in to map

Laevorotation answered 2/8, 2014 at 14:12 Comment(1)
Works like charm, thanks very much you saved my day :)Pantaloons
C
12

In Volley Request Class it calls encodeParameters method which called by getBody() method.

In getBody() method it check for all the params as

 public byte[] getBody() throws AuthFailureError {
        Map<String, String> params = getParams();
        if (params != null && params.size() > 0) {
            return encodeParameters(params, getParamsEncoding());
        }
        return null;
    }

but not for individual parameters.So you have to check for null-ability in individual parameters in getParams() method like

  protected Map<String, String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("tag", "SHOW_FILE");
            if(filename != null)params.put("filename",filename);
            return params;
        };
Circumbendibus answered 2/8, 2014 at 5:33 Comment(2)
Is volley don't support to pass null value?Laevorotation
@VinilChandran In encodeParameters method is not given for null value check.For more info see github.com/mcxiaoke/android-volley/blob/master/src/com/android/…Circumbendibus

© 2022 - 2024 — McMap. All rights reserved.