0

I'm writing an Android app and I want to send some JSON data to a PHP server. The POST request does go to the server but in my server.php script I check the $_POST variable and it is empty. TCP/IP monitor is not in Eclipse ADT and wireshark doesn't show localhost request so I can't see what is actually being sent. So does anyone have an idea what is being sent and how I can access it in PHP? Or have I made a mistake in the code somewhere?

   JSONObject json = new JSONObject();

    try {
        json.put("dog", "cat");
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    HttpURLConnection urlConnection = null;
    try {
        URL url = new URL("http://10.0.2.2/server.php");
        urlConnection = (HttpURLConnection)url.openConnection();
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Accept", "application/json");
        urlConnection.setRequestMethod("POST");         
        urlConnection.setDoOutput(true);
        OutputStreamWriter os = new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8");
        os.write(json.toString());
        os.close();
    }
7
  • What response do you get from the server ? Commented Jan 2, 2013 at 2:26
  • Check the $_REQUEST to see what u get. Commented Jan 2, 2013 at 2:30
  • @t0s It's sending me back the HTML for the server.php page. Commented Jan 2, 2013 at 2:43
  • Code looks good ... grab Charles monitoring software from charlesproxy.com/download and set up its proxy to point to your web server. That should allow you to catch localhost requests and view their contents. Commented Jan 2, 2013 at 2:51
  • 1
    Someone deleted their response to me...their reply was correct, once I set urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") the data is showing up on the server. Commented Jan 2, 2013 at 2:55

3 Answers 3

5

try it.

JSONObject json = new JSONObject();

    try {
        json.put("dog", "cat");
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

HttpClient localDefaultHttpClient=new DefaultHttpClient();
        HttpPost lotlisting = new HttpPost("http://10.0.2.2/server.php");
        ArrayList localArrayList = new ArrayList();
        localArrayList.add(new BasicNameValuePair("json",json.toString()));
        try {
            lotlisting.setEntity(new UrlEncodedFormEntity(localArrayList));
            String str = EntityUtils.toString(localDefaultHttpClient.execute(lotlisting).getEntity());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

you will get the output in str variable;

Sign up to request clarification or add additional context in comments.

Comments

1

I think is missing the flush Try os.flush(); after os.write(..)

Comments

1

Don't use $_POST. Do something like this on user server

    $json = file_get_contents('php://input');
    $animals= json_decode($json,true);

    //you can do 
    echo $animals['dog'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.