凌空串请求错误,而通过字符串以空值作为参数 [英] Volley string request error while passing string with null value as param
本文介绍了凌空串请求错误,而通过字符串以空值作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用下面的code上凌空字符串请求
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");
如果变量名= NULL,我收到以下错误:
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)
时有需要的所有参数我张贴或任何其他方法一个空值检查来解决这个问题,并与NULL值处理?
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 ?
推荐答案
在<一个href=\"https://github.com/mcxiaoke/android-volley/blob/master/src/com/android/volley/Request.java\">Volley请求类它调用连接codeParameters
方法,它通过称为getBody()
方法。
In Volley Request Class it calls encodeParameters
method which called by getBody()
method.
在 getBody()
方法是检查所有的 PARAMS
为
public byte[] getBody() throws AuthFailureError {
Map<String, String> params = getParams();
if (params != null && params.size() > 0) {
return encodeParameters(params, getParamsEncoding());
}
return null;
}
但不是必须检查在个别参数的空能力单独parameters.So在 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;
};
这篇关于凌空串请求错误,而通过字符串以空值作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文