我正在开发一个Android应用程序,您应该注册为驱动程序或骑手。我使用排球库存储用户'关于数据库的信息,现在当我注册为骑手时,一切正常。但是,当我尝试注册为驱动程序时,它会抛出一个没有消息的异常,因此我甚至不知道该交易是什么。我在类中编写了构造函数,它扩展了StringRequest。使用散列映射,在两种情况下都使用具有相同键名的值:驱动程序和骑手。 我使用了一个区分两种情况的开关。打开后,您将注册为驱动程序。否则,您将注册为骑手。
这里是写入请求构造函数的PostRequest类:
//The rider register request
public class PostRequest extends StringRequest {
private static final String register_url="http://pickmeapp.netai.net/register.php";
private static final String logIn_url="http://pickmeapp.netai.net/db_functions.php";
private Map<String,String> params;
public PostRequest(String Name,String username,String password, String age,String gcm_id,String userType,Response.Listener<String> listener)
{
super(Method.POST,register_url,listener,null);
params= new HashMap<>();
params.put("regId",gcm_id);
params.put("registerName",Name);
params.put("registerPassword",password);
params.put("registerUserName",username);
params.put("registerAge",age);
params.put("registerUserType",userType);
}
public PostRequest(String username,String password,Response.Listener<String> listener)
{
super(Method.POST,logIn_url,listener,null);
params=new HashMap<>();
params.put("UserName",username);
params.put("Password",password);
}
//The driver register request constructor
public PostRequest(String Name,String username,String password, String age,String gcm_id,String carNumber,String carModel,String userType,Response.Listener<String> listener)
{
super(Method.POST,register_url,listener,null);
params= new HashMap<>();
params.put("regId",gcm_id);
params.put("registerName", Name);
params.put("registerPassword",password);
params.put("registerUserName",username);
params.put("registerAge",age);
params.put("registerCarNumber",carNumber);
params.put("registerCarModel",carModel);
params.put("registerUserType",userType);
}
public Map<String,String> getParams()
{
return params;
}
}
这是我将数据发送到服务器的代码:
private void WebServerRegistrationTask() {
String name,username,age,password;
PostRequest registerRequest;
name=nameEditText.getText().toString();
username=userNameEditText.getText().toString();
age=ageEditText.getText().toString();
password=passwordEditText.getText().toString();
final Response.Listener<String> gcm_database_listener= new Response.Listener<String>()
{
public void onResponse(String response) {
Intent intent = new Intent(RegisterActivity.this,LogIn.class);
startActivity(intent);
}
};
try {
if(aSwitch.isChecked())
{
carNumberString= carNumberEditText.getText().toString();
carModelString=carModelEditText.getText().toString();
userType="driver";
Log.i("Car Number",carNumberString);
Log.i("Car Model", carModelString);
registerRequest=new PostRequest(name,username,password,age,gcmRegId,carNumberString,carModelString,userType,gcm_database_listener);
}
else {
userType = "rider";
registerRequest = new PostRequest(name, username, password, age, gcmRegId, userType, gcm_database_listener);
}
RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);
requestQueue.add(registerRequest);
} catch(Exception ex)
{
Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
应该提到的是,当我尝试使用logcat找到异常时,它给出了一个NullPointerException:
log.e("Error is",ex.getMessage());
答案 0 :(得分:1)
第一个问题是你没有在凌空上设置错误监听器!在PostRequest的超级电话中
super(Method.POST,register_url,listener,null);
null应该是你的错误监听器,所以你知道为什么出错了。试试这个PostRequest类
public class PostRequest extends StringRequest {
private static final String register_url="http://pickmeapp.netai.net/register.php";
private static final String logIn_url="http://pickmeapp.netai.net/db_functions.php";
private Map<String,String> params;
public PostRequest(String Name,String username,String password, String age,String gcm_id,String userType,Response.Listener<String> listener, Response.ErrorListener errorListener)
{
super(Method.POST,register_url,listener,errorListener);
params= new HashMap<>();
params.put("regId",gcm_id);
params.put("registerName",Name);
params.put("registerPassword",password);
params.put("registerUserName",username);
params.put("registerAge",age);
params.put("registerUserType",userType);
}
public PostRequest(String username,String password,Response.Listener<String> listener, Response.ErrorListener errorListener)
{
super(Method.POST,logIn_url,listener,errorListener);
params=new HashMap<>();
params.put("UserName",username);
params.put("Password",password);
}
//The driver register request constructor
public PostRequest(String Name,String username,String password, String age,String gcm_id,String carNumber,String carModel,String userType,Response.Listener<String> listener, Response.ErrorListener errorListener)
{
super(Method.POST,register_url,listener,errorListener);
params= new HashMap<>();
params.put("regId",gcm_id);
params.put("registerName", Name);
params.put("registerPassword",password);
params.put("registerUserName",username);
params.put("registerAge",age);
params.put("registerCarNumber",carNumber);
params.put("registerCarModel",carModel);
params.put("registerUserType",userType);
}
public Map<String,String> getParams()
{
return params;
}
}
然后在创建请求时也会创建一个错误lsitener并将其传递给:
final Response.Listener<String> gcm_database_listener= new Response.Listener<String>()
{
public void onResponse(String response) {
Intent intent = new Intent(RegisterActivity.this,LogIn.class);
startActivity(intent);
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError e) {
String error = e.networkResponse.statusCode + ": " + new String(e.networkResponse.data);
Log.e("Volley Error: ", error);
}
};
try {
if(aSwitch.isChecked())
{
carNumberString= carNumberEditText.getText().toString();
carModelString=carModelEditText.getText().toString();
userType="driver";
Log.i("Car Number",carNumberString);
Log.i("Car Model", carModelString);
registerRequest=new PostRequest(name,username,password,age,gcmRegId,carNumberString,carModelString,userType,gcm_database_listener,errorListener);
}