我一直在使用Android Volley
库来对我服务器中的PHP脚本执行一段时间的请求。现在,当我这样做时,我收到了一个不寻常的错误。
我调用脚本的Android代码如下:
private void writeDatatoDB(){
StringRequest sr = new StringRequest(Request.Method.POST,URL , new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> maps = new HashMap<>();
maps.put(SinchHolders.phpUserName,uName);
maps.put(SinchHolders.phpUserNickName,uNick);
maps.put(SinchHolders.phpUserPassword,uPass);
maps.put(SinchHolders.phpUserFirebaseToken,uFCM);
maps.put(SinchHolders.phpUserProfession,uUserProf);
maps.put(SinchHolders.phpUserexpertise,uUserExp);
maps.put(SinchHolders.phpUserGender,ugender);
maps.put(SinchHolders.phpUserZip,uZip);
maps.put(SinchHolders.phpUserBirthYear,uBirth);
return maps;
}
};
RequestQueue rq = Volley.newRequestQueue(dummy_signup_2.this);
rq.add(sr);
}
Volley
库的PHP脚本如下:
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST' ){
$id = $_POST['UserName'];
$nick =$_POST['NickName'];
$pass =$_POST['UserPassword'];
$FirebaseToken = $_POST['FireBaseId'];
$BDate = $_POST['bithdate'];
$ZCode = $_POST['zipcode'];
$Gender = $_POST['sex'];
$UserProfession = $_POST['userprofession'];
$userexpert = $_POST['userexpertise'];
$sql="insert into `User`(`UserNickName`, `UserName`, `password`, `createDate`, `FirebaseToken`, `BirthYear`, `ZipCode`, `Gender`, `UserProfession`, `UserExpertise`) values ('$nick','$id','$pass',Default,'$FirebaseToken','$BDate','$ZCode','$Gender','$UserProfession','$UserExpertise')";
require_once('db_connect_me.php');
if(mysqli_query($con,$sql)){
echo"Success";
}
else{
echo "Failure";
}
mysqli_close($con);
}else{
echo "Error";
}
?>
Volley
给我的错误是com.android.volley.VolleyError: Java.Lang.NullPointerException
。该错误是由服务器端的一些错误引起的,因为错误在ErrorListener
中定义的toast中显示。我不明白问题可能是什么。我已经创建了一个简单的helloworld php文件,并使用相同的代码调用它,我能够获得输出。这有什么可能是错的?
StackTrace:
01-27 19:11:41.715 12548-16431/com.example.****.****E/Volley: [2720] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
java.lang.NullPointerException
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at com.android.volley.Request.encodeParameters(Request.java:450)
at com.android.volley.Request.getBody(Request.java:436)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:260)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
PS:请仔细阅读此问题,您会理解这与本网站上的NullPointerException
个问题不重复。
答案 0 :(得分:2)
当任何exception
值为null时,您将获得此params
。检查params
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//put your map keys
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 :(得分:1)
NullPointException
是由一个值绑定到DB并且没有从用户获取值的值引起的。缺少 - createDate
:
进入DB的参数是10:
`UserNickName`, `UserName`, `password`, `createDate`, `FirebaseToken`, `BirthYear`, `ZipCode`, `Gender`, `UserProfession`, `UserExpertise`
您错过了输入值:
`createDate`
仅限用户9:
maps.put(SinchHolders.phpUserName,uName);
maps.put(SinchHolders.phpUserNickName,uNick);
maps.put(SinchHolders.phpUserPassword,uPass);
maps.put(SinchHolders.phpUserFirebaseToken,uFCM);
maps.put(SinchHolders.phpUserProfession,uUserProf);
maps.put(SinchHolders.phpUserexpertise,uUserExp);
maps.put(SinchHolders.phpUserGender,ugender);
maps.put(SinchHolders.phpUserZip,uZip);
maps.put(SinchHolders.phpUserBirthYear,uBirth);