0

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.

6
  • data is an object, needs to be something like data = {"str":"data"} Commented Oct 4, 2014 at 0:40
  • @kellycode data is a string in both the js code and my java code, I just stored it as a variable. Commented Oct 4, 2014 at 0:46
  • For the love of all that is sacred, please, do not let your catch empty, at least do some Log.e(), otherwise you will never ever know if something is going wrong. Commented Oct 4, 2014 at 0:47
  • @AlvarezAriel I'm still a noob, please bear with me :) Commented Oct 4, 2014 at 0:49
  • It was all with good intentions. Now you know that leaving a catch empty is generally a back practice, and for your own good you should add at least some logging. Probably that is all we need to figure out what went wrong with this code. Use developer.android.com/tools/debugging/debugging-log.html Commented Oct 4, 2014 at 0:54

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

This is not what the user asks, he wants to make his code work on android, in java. The example in php is just to show us what he is trying to do.

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.