0

I know this question was asked before. Please don't mark as duplicate or block my account.

I am trying to send Json object to php using volley. So that I am using Customrequest which I have found a solution in stackoverflow but it didn't work See this

JsonObject response = new JsonObject();
response.add("Description", a1 );/*a1 is Json array of multiple values*/
response.add("Amount", a2 );/*a2 is Json array of multiple values*/
System.out.println("Json:"+response);/*Both the list is saved inside this Json object*/

Map<String,String> hm = new HashMap();
hm.put("Values",String.valueOf(response));/*Trying to pass the json object to server respose is the Json object*/

CustomRequest req = new 
CustomRequest(Request.Method.POST,ip,hm, this.createRequestSuccessListener(), this.createRequestErrorListener()); /*I have used the solution here*/

Mysingleton.getInstance(this).addTorequestque(req);/*This is a singleton class*/
public Response.Listener<JSONObject> createRequestSuccessListener(){
    return new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            AlertDialog.Builder al = new AlertDialog.Builder(Database.this)
                .setTitle("Message")
                .setMessage("Sent")
                .setPositiveButton("OK",null);
            AlertDialog al1 = al.create();
            al.show();
        }
    };
}

public Response.ErrorListener createRequestErrorListener(){
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            AlertDialog.Builder al = new AlertDialog.Builder(Database.this)
                .setTitle("Error")
                .setMessage("Something went wrong")
                .setPositiveButton("OK",null);
            AlertDialog al1 = al.create();
            al.show();
        }
    };
}

My php source code is here

if(!empty($_POST['Values']){
    $all_arraylist = json_decode(Values,true);
    $ result = print_r($all_arraylist,true);
    echo $result;
}
else{
    echo "Empty";
}

expecting at least error message to pop up but I am unable to get even that. That is I am not getting any response. Please help

EDToaster
  • 3,160
  • 3
  • 16
  • 25

1 Answers1

0

I can't say anything about your Java code, but your PHP code is not valid at all. This usually results in a blank screen being served back to the client without any content at all.

if(!empty($_POST['Values']){
    $all_arraylist = json_decode(Values,true);

You probably mean json_decode($_POST['Values'], true); here, since just Values by itself doesn't make any sense.

    $ result = print_r($all_arraylist,true);

The space after $ will confuse PHP, as that isn't a valid variable name character (and you're not using $ result on the next line either).

(and instead of using true to return the value from print_r and then echo that returned value, you can just use print_r($all_arraylist) to make it print out everything.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • Ok to confirm that I have received that message I am just putting the statement as "echo"Received"; instead of $all_arraylist = json_decode(Values,true); $ result = print_r($all_arraylist,true); echo $result; but still it is not working – sammu darling Jun 27 '19 at 12:17
  • _Where_ are you looking for that output? You can also use `print_r($_POST);` or `var_dump($_POST);` to see everything sent as part of the POST request. – MatsLindh Jun 27 '19 at 12:35
  • I am looking the output in android not in php. – sammu darling Jun 27 '19 at 13:06
  • If my Json object is sent successfully, then it has to print "Received" in android – sammu darling Jun 27 '19 at 13:07
  • There is nothing in your Android code indicating that `Received` would be output at any time as far as I can tell. You can use something like Wireshark to see what happens between the emulator and your PHP service. – MatsLindh Jun 27 '19 at 13:28
  • Please see the above code, I have defined a method "public Response.Listener createRequestSuccessListener()" to display message – sammu darling Jun 28 '19 at 10:16
  • That's correct, but there is nothing in that code that prints `Received` or anything else from the response. Use wireshark to see what's actually happening behind the scenes, or adding proper logging to a log file in your PHP application. – MatsLindh Jun 28 '19 at 10:31