I have been trying to send message from the android emulator to the
web server by using HttpURLConnection. In the server side I use php to
read the sent message and keep it in database (mysql). when I send the
message, a blank row is added in the database, but the message sent
doesn't appear. I think the emulator can communicate with the server
but I don't know what happens to the message. Please help me.

Here is my Android and PHP code:

        URL url = new URL("http://10.0.2.2/receiveSampleAndroid.php";);

        HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
        httpCon.setDoInput(true);
        httpCon.setDoOutput(true);
        httpCon.setRequestMethod("POST");
        httpCon.setUseCaches(false);
        httpCon.setRequestProperty("Connection", "Keep-Alive");
        DataOutputStream out = new DataOutputStream(httpCon.getOutputStream
());
        DataInputStream in      = new DataInputStream(httpCon.getInputStream());

        String value = "msg content";
        String Boundary = "`";
        out.write(value.getBytes());
        out.write(Boundary.getBytes());
        out.flush();

        int responseCode = httpCon.getResponseCode();

        switch(responseCode)
        {
                case HttpURLConnection.HTTP_OK:
                case HttpURLConnection.HTTP_CREATED:
                        Toast.makeText(getBaseContext(), "Message is sent",
Toast.LENGTH_LONG).show();
                        int httpReslut = 1;
                        int ch;
                        in.readByte();
                        in.readByte();
                        in.readByte();
                        break;

                default:
                        //not ok
        }

        httpCon.disconnect();
        in.close();
        out.close();


My PHP Code is:


<?php

if(isset($_POST))
{

$string = file_get_contents('php://input');
$boundary = "`";
$data = explode($boundary,$string);


/**********************************************************
Content of the Message is Store in local variable
***********************************************************/

$id = $data[0];

$name = $data[1];

$fname = $data[2];


/**********************************************************
Connect to database, Sample_android and

***********************************************************/

mysql_connect("localhost","root","root") or die("Server Connection
Error");
mysql_query("use sample_android") or die("Can not communicate with the
database.");

//Insert patient record in patient_record table
$sql_InsertPatientRecord = "insert into sample(id, name, fname) values
('$id', '$name', '$fname')";
mysql_query($sql_InsertPatientRecord);
mysql_close();
echo "Message is inserted";

}

?>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to