我发送带有一些参数的字符串POST请求,但在PHP脚本中,我总是将参数值作为null . 当我用邮差测试PHP工作正常 .
这是我的截击请求代码:
public void login(final String mPhone, final String mEmail, final String mPassword) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getBoolean("error")) {
String error = jsonObject.getString("message");
if (error.equals("new user")) {
registerUser();
} else {
showProgress(false);
Snackbar.make(mOtpView, error, Snackbar.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
showProgress(false);
Snackbar.make(mOtpView, error.getMessage(), Snackbar.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("phone", mPhone);
params.put("email", mEmail);
params.put("password", mPassword);
return params;
}
};
//Method to limit retry policy of request
stringRequest.setRetryPolicy(
new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2,
3,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
);
try {
//Adding request to request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
这是我的PHP:
require_once 'DbConfig.php';
$response = array();
if (isset($_GET['apicall'])) {
switch ($_GET['apicall']) {
case 'login':
$phone = $_POST['phone'];
$email = $_POST['email'];
$pass = $_POST['password'];
echo $phone.$email.$pass;exit();
我试图通过覆盖getHeaders()来尝试添加标头,并尝试重写getBodyContent()但似乎没有任何工作,并且上面的代码在我在localhost中使用它时工作正常 . 我检查了URL的罚款,因为PHP脚本很好,因为我已经通过回显其他执行得很好的东西来测试它 .
2 回答
你只是发送;
params.put("phone", mPhone); params.put("email", mEmail); params.put("password", mPassword);
怎么样;
params.put("apicall","YOu_API");
我猜
apicall
不是isset,所以值将为null .Usauly这个问题可能是从Content-Type添加charset = utf-8到它:
图片来源:Volley-POST-EMPTY