将null值作为参数传递给字符串时,排列字符串请求错误

时间:2014-08-02 05:14:17

标签: android null params android-volley

我在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");

如果变量filename = null,我收到以下错误:

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)

是否需要对所有param I POST或任何其他方法进行空值检查以解决此问题并使用NULL值进行处理?

2 个答案:

答案 0 :(得分:21)

感谢Giru Bhai的帮助

我创建了一个新方法来解决这个问题,而变量变为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;             
        }

我认为这比在将每个值放入地图之前检查每个值更容易

答案 1 :(得分:12)

Volley Request Class中,它调用encodeParameters方法调用getBody()方法。

getBody()方法中,它会检查所有params

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

但不适用于个别参数。所以你必须在getParams()方法中检查个别参数的无效能力,如

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