我一直在与Volley一起为该项目编写代码,其他所有类/片段都在工作。由于某种原因,这不是。我进行了一些搜索,得出的结论是我的参数中某处有一个NULL值,但我使用log跟踪了每个输入,甚至进行了一些调试。
我不知道这里是什么问题。我的其他片段工作正常,但是这只是拒绝将代码发送到服务器上的PHP脚本。我已经测试了后端与Postman可以正常工作,并且获得了成功的回报,但是与Volley一起却无法正常工作。
public void send()
{
// assign values from our text inputs to variables
ReturnedUseByDateEntered = this.useByDate.getText().toString().trim();
batchCodeEntered = this.batchCode.getText().toString().trim();
returnedDate = this.finalDate.getText().toString().trim();
signatureEntered = SessionHandler.getInstance(getActivity().getApplicationContext()).getUserName();
StringRequest stringRequest = new StringRequest(StringRequest.Method.POST, URL_INSERT, new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Log.d("DELIVERY",response);
JSONObject jsonObject = null; // declare JSON object to hold the status
try
{
jsonObject = new JSONObject(response); // get the response and store it into the JSONobject
// Get the response from PHP script on server,
// If response of the success vairable is equal to 1
// Store the return from the PHP scripts of the error tag, if its equal to false (means no error)
String result = jsonObject.getString("success");
Log.e("Response", response.toString());
if(result.equals("1"))
{
Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e)
{
e.printStackTrace();
Toast.makeText(getActivity(), "Authentication Error " + e.toString(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getActivity(), "Authentication Failed " + error.toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String, String> params = new HashMap<>();
params.put("date", returnedDate);
params.put("food_item", foodItemEntered);
params.put("batch_code", batchCodeEntered);
params.put("supplied_by", supplierEntered);
params.put("use_by_date", ReturnedUseByDateEntered);
params.put("temp", tempEntered);
params.put("rejected", String.valueOf(rejected));
params.put("comment", commentEntered);
params.put("signature", signatureEntered);
return params;
}
};
// This line uses the singleton method ( Session Handler Class )
// It is said to use a singleton when an application makes constant use of a network
MySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
}
这是我的send方法,当按钮被触发时被调用。 注意::其余值在此方法之外分配。但是我已经在其他项目中对其进行了测试,并且可以正常工作