-2

I've developed a Application that i'm going to send data from the phone to the server by Json, to do this i use Volley Library in Android.
but i can not send data to the server!

my simple php code :

$name = $_GET["name"];
$j = array('name' =>$name);
echo json_encode($j);

my java code :

    private void makeJsonObjectRequest() {
        mRequestQueue = Volley.newRequestQueue(this);
        String url = "http://my-site-name/sampleGET.php";

        StringRequest jsonObjReq = new StringRequest(
            Request.Method.GET, 
            url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("result:", response);
                    Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                }
            }, 
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("err", "Error: " + error.getMessage());
                    makeJsonObjectRequest();
                }
            }
        ){
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("name", "json");
                return params;
            }
        };
        mRequestQueue.add(jsonObjReq);
    }

It also got help from the link below : Click to see link

But still could not use it and send data (POST or GET) from mobile to server!!!
How can I do this?

Community
  • 1
  • 1
dashakhe.goli
  • 59
  • 2
  • 7
  • If your network connection fails, you'll enter a recursive loop - remove makeJsonObjectRequest(); - in the onErrorResponse method. – Zain Jun 25 '15 at 14:04
  • Without any logs etc. I'd be inclined to say you could try $_SERVER["name"] instead of $_GET["name"] – Zain Jun 25 '15 at 14:05

1 Answers1

0

As advised on How to send json from android to php?

From current code, remove this method:

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("name", "json");
            return params;
        }

And override the getBody() method instead. Then, adapt it to return the json data that you need as request body based on the below sample:

    public byte[] getBody() throws AuthFailureError {
        JSONObject js = new JSONObject();
        try {
               js.put("fieldName", "fieldValue");
        } catch (JSONException e) {
               e.printStackTrace();
        }

        return js.toString().getBytes();
    }

And then in php to decode your json data:

    $jsonRequest = json_decode(stream_get_contents(STDIN));
    var_dump($jsonRequest);

Please let me know if more information is needed.

Community
  • 1
  • 1
Andrei Mărcuţ
  • 1,273
  • 17
  • 28