【发布时间】:2015-07-25 12:13:22
【问题描述】:
每次我尝试对 Volley 使用 POST 方法时,都会出现服务器错误。我在 getCause 中得到空值,在 getNetworkResponse.toString() 中得到一些默认值。
如果我使用 GET 方法,这可以正常工作(我从我的 url 获得响应)。
谁能帮我做点什么?
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("teststr", "abd");
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
url,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(), "Success"+response.toString(), Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "JSON ERROR", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("abd", "Error: " + error
+ ">>" + error.networkResponse.statusCode
+ ">>" + error.networkResponse.data
+ ">>" + error.getCause()
+ ">>" + error.getMessage());
}
}) {
@Override
protected Map<String,String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("key", "value");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
requestQueue.add(request);
错误日志:
错误: 错误:com.android.volley.ServerError>>404>>[B@42b1e0d0>>null>>null
更新: networkResponse.statusCode 为 404,尽管 url 是可访问的(如果我只使用 GET 方法,则返回数据)。如果我在 POST 方法中删除标题部分,仍然一样。
网址:
<?php
$response = array();
$jsonString = file_get_contents('php://input');
$jsonObj = json_decode($jsonString, true);
if(!isset($jsonObj['teststr'])){
$response["msg"] = "No data.";
}else{
$response["msg"] = "Success: ".$jsonObj['teststr'];
}
echo json_encode($response);
?>
【问题讨论】:
-
你检查过error.getMessage(); ?
-
是的,这也是空的
-
你有解决办法吗,我迷路了,同样的错误,用邮递员测试时服务器响应正常