Send json data from client to server using volley library

Hi!!

I want to send a simple message from client to server (the server is not localhost). I use Volley library and the method POST.
When I run the code did not make the connection, LogCat prints 2.onErrorResponse: Error:

Client code:

String url = "http://myWebServer.eu";
RequestQueue queue;
JsonObjectRequest request;
Map<String, String> map = new HashMap<String, String>();

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    queue = Volley.newRequestQueue(this);

    map.put("param1", "example");

    request = new JsonObjectRequest(Request.Method.POST, 

            url, 
            new JSONObject(map), 
            new Response.Listener<JSONObject>() { 
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        VolleyLog.v("Response:%n %s", response.toString(7));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() { 
                @Override
                public void onErrorResponse(VolleyError error) {

                    VolleyLog.e("Error: ", error.getMessage());
                }
            });

    queue.add(request);
}

PHP Server code

$jsondata = $_POST[‘param1’];

$json = json_decode($jsondata,true);

echo $json ;

Can someone help?


Source: json

1 Comment

  1. Edwin

    Here you try to decode non json type into json. The $post[‘param1’] is the string value which you can request form the your app to server just print $post[‘param1’] is the String format not a json

    $jsondata = $_POST['param1'];
    
    echo $json_encode($_POST)  ;
    Reply

Leave a Reply