0

I am trying to send data to php side from my Android client below is the code:

(I have constructed this code from various sources on the internet so I am not sure if all is well here)

private void postJSON(String myurl) throws IOException {
        java.util.Date date= new java.util.Date();
        Timestamp timestamp = (new Timestamp(date.getTime()));
        try {
            JSONObject parameters = new JSONObject();
            parameters.put("timestamp",timestamp);
            parameters.put("jsonArray", new JSONArray(Arrays.asList(makeJSON())));
            parameters.put("type", "Android");
            parameters.put("mail", "xyz@gmail.com");
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //  conn.setReadTimeout(10000 /* milliseconds */);
            //  conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestProperty( "Content-Type", "application/json" );
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            OutputStream out = new BufferedOutputStream(conn.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
            writer.write(parameters.toString());
            writer.close();
            out.close();

            int responseCode = conn.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println(response.toString());

        }catch (Exception exception) {
            System.out.println("Exception: "+exception);
        }
    } 

I am confused at this line:

writer.write(parameters.toString());

essentially I am only sending one string to the php side. How do I receive it there? What would be the POST variable name?

User3
  • 2,465
  • 8
  • 41
  • 84
  • 2
    You're thinking of form fields, which have names. In this case, you're POSTing your JSON string. It doesn't have a name. [This answer](http://stackoverflow.com/a/22662113/622391) tells you how to receive JSON content. – Simon MᶜKenzie Jan 21 '15 at 05:07
  • I would suggest pass some parameters and do some processing on those parameter, then return them, On android end, get those parameter and see if you're getting those processed parameter or not. If you get then you're successfully posting the parameter. – Gaurav Dave Jan 21 '15 at 05:22
  • @GauravDave that is exactly what I am trying to do :) – User3 Jan 21 '15 at 05:29
  • Awesome, so are you getting processed parameters? or something weird is occurring? – Gaurav Dave Jan 21 '15 at 05:32
  • @GauravDave Yes: `Response Code : 200 01-21 10:46:44.969: I/System.out(21561):
    Catchable fatal error: Object of class stdClass could not be converted to string in /opt/lampp/htdocs/Pdrive/digital_anniversaries/wordpress-3.9.1/wordpress/auth‌​enticate_user.php on line 15
    `
    – User3 Jan 21 '15 at 05:36
  • can you please add your php – Gaurav Dave Jan 21 '15 at 05:40

3 Answers3

1

I have absolutely no experience with android, however I am guessing either an array of some kind or as json, You can see what it sends by making a page with <?php echo var_dump($_POST); ?> on it and sending the POST request to the page.

CarlosAllende
  • 292
  • 3
  • 11
  • I am getting a `array(0) {}` as echo if I use the above. – User3 Jan 21 '15 at 05:21
  • Hmm, are you sure it is sending the POST data correctly? Try `` as suggested in the answer Simon MᶜKenzie linked – CarlosAllende Jan 21 '15 at 05:23
  • When I do what was suggested by @simon I get `Response Code : 200 01-21 10:46:44.969: I/System.out(21561):
    Catchable fatal error: Object of class stdClass could not be converted to string in /opt/lampp/htdocs/Pdrive/digital_anniversaries/wordpress-3.9.1/wordpress/authenticate_user.php on line 15
    `
    – User3 Jan 21 '15 at 05:25
  • Sorry, No idea what that means. I hope someone else can help you – CarlosAllende Jan 21 '15 at 05:34
1

It looks like you are sending binary data instead of name-value post parameters.

Try this in you php code : echo file_get_contents("php://input"); and see what it prints.

additionally, You can either use HTTPClient or RestHTTPClient from loopj as you android client.

Raja
  • 851
  • 9
  • 22
  • I have been using a default HTTP Client and it works fine. I just wanted to try it with HTTPUrl Connection. Thansk for the advice :) – User3 Jan 21 '15 at 06:32
1

like CarlosAllende i have no experience in Android. But hope this might help you.

this is what you get in your '$_POST' ?

 Response Code : 200 01-21 10:46:44.969: I/System.out(21561): <br /> 
 <b>Catchable fatal error</b>: Object of class stdClass could not be converted to string in 
<b>/opt/lampp/htdocs/Pdrive/digital_anniversaries/wordpress-3.9.1/wordpress/auth‌​enticate_user.php</b> on line <b>15</b><br /> 

if so it is a Catchable fatal error telling that 'Object of class stdClass could not be converted to string'. so if i am correct it's trying to take object into your $_POST array and it's is unable to convert it to string. and instead your getting the error in your $_POST array.

hope this helps

vishal
  • 55
  • 6
  • 1
    Thanks a ton @Vishal. I am starting to convert the whole method into name value pairs now for better readability and clean code :) – User3 Jan 21 '15 at 06:44