用空值作为parameter passingstring时排空string请求错误

我在volley上使用下面的代码string请求

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

如果variablesfilename = 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) 

是否需要一个空值检查所有参数我POST或任何其他方法来解决这个问题,并处理NULL值?

  • Android 2.1 View的getDrawingCache()方法始终返回null
  • 大内存块不是垃圾收集
  • 错误:条目中的空值:blameLogFolder = null
  • 我们如何在Android中将null定义为数据types,就像我们在iPhone中定义nsNull一样
  • 无法将null传递给execute(); android 4.0中的AsyncTask方法
  • Android日历获取当前星期几作为string
  • java.lang.Void vs void vs Null
  • android BluetoothDevice.getName()返回null
  • 感谢Giru Bhai的帮助

    我创build了一个新的方法来解决这个问题,而variables为空

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

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

    在Volley请求类中,它调用由getBody()方法调用的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()方法中的个别参数中检查null-ability

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