Ogni volta che provo a utilizzare il metodo POST con Volley, viene visualizzato un errore grave. Ottengo valore null in getCause e un valore predefinito in getNetworkResponse.toString().Errore server Volley con null Risposta di rete
Se utilizzo il metodo GET, funziona correttamente (ottengo risposta dal mio URL).
Qualcuno può aiutare cosa posso fare?
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);
Registro errori:
Error: Error: com.android.volley.ServerError>>404>>[[email protected]>>null>>null
UPDATE: networkResponse.statusCode viene come 404, se l'url è accessibile (e restituire dati se mi basta usare il metodo GET). Se rimuovo la parte di intestazione nel metodo POST, è sempre la stessa.
l'url:
<?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);
?>
Hai controllato error.getMessage(); ? – theapache64
Sì, anche questo è nullo – abdfahim
hai una soluzione, sono perso con lo stesso errore e il server risponde normalmente quando testato con postino –