0

I am new to both PHP and Android.

I am doing this request like this in PHP which works well. The "name" will contain a variable like "upperLight" so that we can compare with the android code.

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_connect($sock,"192.168.2.104", 80);

$msg = 'a';
if (isset($_POST["name"])){
    $msg= $_POST["name"] ;
}

socket_write($sock, $msg);

I am trying to do the same in android like this but its not working.

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("upperLight","1"));

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URLTest);
        post.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse execute = client.execute(post);

On the other end (192.168.2.104) i am checking for the word "upperLight" in the request.

EDIT : the variable URLTest contains - "192.168.2.104:80"

drinu16
  • 775
  • 2
  • 11
  • 25
  • Look at this response http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post – Kiril Aleksandrov Oct 09 '14 at 20:20
  • `The "name" will contain a variable like "upperLight"`. No. The variable $_POST['name'] wil have the value 'upperLight'. Which you could have send with `new BasicNameValuePair("name", "upperLight")`. – greenapps Oct 09 '14 at 20:22
  • `the variable URLTest contains - "192.168.2.104:80"`. That will not work. You need to specify the protocol too: "http://192.168.2.104:80"`. – greenapps Oct 09 '14 at 20:26

2 Answers2

0
  • try this

    URL url = new URL("http://192.168.2.104:80");
    URLConnection con = url.openConnection();
    con.setDoOutput(true);
    OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
    out.write( "upperLight" );
    out.flush();
    
ashoke
  • 6,441
  • 2
  • 26
  • 25
0

The PHP code is connecting to a TCP socket on port 80, while the Android code is connecting to an HTTP server.

Those are two completely different things. If you need to port the PHP code to Java/Android, you would start by using the corresponding library, which is Socket.

x-code
  • 2,940
  • 1
  • 18
  • 19