我的应用程序中有一个登录屏幕。我想从输入字段中检索电话号码并将其作为带有 volley 的发布请求发送到我的后端服务器。后端服务器的结构是这样的:
"user": {
"phone": 909099999
}
我试过这段代码:
final String phone = login_phoneTET.getText().toString();
Map<String, String> params = new HashMap<>();
params.put("phone", phone);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
Constants.TING_OTP_ENDPOINT,
new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Success");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Failed with an Error" + error.getMessage());
error.printStackTrace();
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(20000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getVolleySingleton(this).addToRequestQueue(jsonObjectRequest);
还尝试使用字符串请求。我不断在 logcat 中收到此错误:
E/Volley: [22489] BasicNetwork.performRequest: Unexpected response code 500 for https://:):)/api/user/generateOTP
我是否正确地提出了这个请求,或者是否有更好的方法来做到这一点?谢谢。
最佳答案
这是服务器端错误。所以我认为你需要关注你的服务器。
关于android - 使用 Volley Android 向服务器发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46291458/