Annoynymoousrsa
Annoynymoousrsa

Reputation: 19

Sending string to php server

I'm trying to send a string to php server from an app, the string should be sent (the method executes without an error), but the server isn't receiving anything. Here's what I'm doing, I followed another SO thread so I'm don't really know if what I'm doing is correct.

    protected String doInBackground(String... params) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            StringEntity se = new StringEntity(data);
            httppost.setEntity(se);
            httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }
        return null;
    }

Here's the code that sends the data to the server in a web app.

     function send_message( message ) {
        data =  "data";
        $.ajax({
          type: 'POST',
          url:"url",
          data: data,
          success: function() {
            $("#message_sent").val( "true" );
          },
          error: function(json) {
          },
          abort: function( json ) {
          }
        });
      }

So basically I want to translate the js code that sends the data into java code and use it in the app.

Upvotes: 0

Views: 354

Answers (1)

KellyCode
KellyCode

Reputation: 797

This is a simple example of the send method:

function send_message(message) {
            var data = {"data":message};
            $.ajax({
                type: 'POST',
                url: "ret.php",
                data: data,
                success: function(returned) {
                    console.log(returned);
                },
                error: function(json) {
                     console.log(json);
                },
                abort: function(json) {
                }
            });
        }

And a simple example of the php in a file called ret.php in the same director as the html page:

<?php
$info = $_POST["data"];
echo $info;

Your success method will get the echo

You could call it with a button:

<button type="button" onclick="send_message('my message');"> Send it</button>

Upvotes: 1

Related Questions