我尝试使用 volley 将 JSON 对象发送到服务器并获得响应,但服务器似乎没有收到数据。
如果我尝试只是回显它有效,但如果我尝试读取服务器上的 JSON 对象并使用react,我会收到截击错误。我也尝试回显 JSON 对象,但仍然出现错误。 当我使用 HttpRequest 时,一切正常。 我添加了与 HttpRequest 和 Volley 中相同的 header ,也许连接到 UTF8?我在 setEntity 上做的
我像这样在 PHP 上获取数据:
$data = json_decode(file_get_contents('php://input'), true);
我的 Volley 代码:
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,
URL, jsonObject ,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG,"Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
params.put("Content-Type", "application/json");
return params;
}
};
;
有人有想法吗?
最佳答案
事实证明,我的截击代码没问题。问题出在服务器端。
使用 Breadbin 注释并将 VolleyLog.e 替换为 error.printStackTrace() 有助于找到问题。
关于php - 服务器在 android 上使用 volley 时不接收 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25553803/