Reputation: 123
Let's start with the code I am using, I have tried every single possible different way to make "params". I have used it as a HashMap, in Json format and also as a string. I have also tried to @Override the getParams() method by creating a hashmap and returning it. Nothing as worked.
Here is my function that calls the JsonObjectRequest.
private void sendJsonObjReq() {
showProgressDialog();
Map<String, String> para = new HashMap<>();
para.put("Condicao", "dea");
para.put("Field", "1550");
JSONObject jason = new JSONObject(para);
String params = jason.toString();
textView.setText(params);
JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST,
url_cond1, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(AppController.TAG, response.toString());
textView.setText(response.toString());
/*try {
int u = response.getInt("sucess");
if(u == 1){
JSONArray json = response.optJSONArray("Type");
if(json != null) {
MakeListHashMap(json);
}else{
textView.setText("Wrong Parameters");
}
}else{textView.setText("success is 0");}
}catch(JSONException e){
Log.e("Error:", e.getMessage());
textView.setText("nothing here");
}*/
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(AppController.TAG, "Error:" + error.getMessage());
showProgressDialog();
}
});
//Add to request queue
AppController.getInstance().addToRequestQueue(jsonReq, tag_json_obj);
}
The url and everything else is fine, i have checked but i just cannot understand why neither the GET or POST method work, since i have tried both but I did not have any success with them. My php code consists of this:
<?php
$response = array();
//$oi = json_decode($_POST);
//$response["Condicao"] = $_GET["Condicao"];
//$response["Field"] = $_GET["Field"];
$response["Condicao"] = $_POST["Condicao"];
$response["Field"] = $_POST["Field"];
$response['sucess'] = 1;
$response['other'] = "test";
echo json_encode($response);
?>
I have tried decoding it and not decoding it, I really am at a loss for what to do. I think it might be a problem with the server but using the App "Postman" I can send GET and POST and the response is a Json array like i want.
If i send key1= Condicao => name=dea; key2= Field => name=1550;
I get the result
{
"Condicao": "dea",
"Field": "1550",
"sucess": 1,
"other": "test"
}
edit: The solution is:
<?php
$_POST = json_decode(file_get_contents('php://input'), true);
$response = array();
$response["Condicao"] = $_POST["Condicao"];
$response["Field"] = $_POST["Field"];
$response['sucess'] = 1;
$response['other'] = "test";
echo json_encode($response);
?>
Upvotes: 3
Views: 1092
Reputation: 37404
1.) Pass your json object instead of string
JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST,
url_cond1, jason ,
// ^^^^
new Response.Listener<JSONObject>() {
2.) Receive it in your php as
<?php
$response = array();
// receive your json object
$jasonarray = json_decode(file_get_contents('php://input'),true);
$response["Condicao"] = $jasonarray["Condicao"];
$response["Field"] = $jasonarray["Field"];
$response['sucess'] = 1;
$response['other'] = "test";
echo json_encode($response);
?>
Upvotes: 2