Data that is sent from android is not being received by PHP. From the looks of things, the android app seems to be connecting to the server just fine. But the $_POST array seems to be empty:
Android:
URL url;
HttpURLConnection urlConnection;
try
{
url = new URL("http://10.0.2.2:80/Web%20/Practice/Android%20Login/phpserver.php");
}catch(IOException ex)
{
ex.printStackTrace();
return "Exception5";
}
try
{
urlConnection = (HttpURLConnection) url.openConnection();
// urlConnection.setReadTimeout(15000);
// urlConnection.setConnectTimeout(10000);
Log.d(TAG, "URL Connection enabled");
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
Log.d(TAG, "Params [0]: " + params[0] + " Params[1]: " + params[1]);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("Username", params[0])
.appendQueryParameter("PhoneNumber", params[1]);
String query = builder.build().getEncodedQuery();
Log.d(TAG, "Query: " + query);
Log.d(TAG, "Append Parameters to URL");
// Open Connection for sending data
OutputStream os = null;
try {
os = urlConnection.getOutputStream();
}catch(Exception ex)
{
ex.printStackTrace();
Log.e("ConcentratorExport", ex.getMessage() );
}
Log.d(TAG, "OutPutStream connection");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
Log.d(TAG, "BufferedWriter established");
writer.write(query);
Log.d(TAG, "Writing to Server");
writer.flush();
writer.close();
os.close();
urlConnection.connect();
Log.d(TAG, "Connected");
}catch(IOException ex)
{
ex.printStackTrace();
return "Exception6";
}
try
{
int response_code = urlConnection.getResponseCode();
if(response_code != 200)
{
Log.d(TAG, "Post failed");
Log.d(TAG, "Post failed with " + response_code);
}
if(response_code == HttpURLConnection.HTTP_OK)
return "Successful!";
else
return "Unsuccessful";
}
catch(IOException ex)
{
ex.printStackTrace();
return "Exception7";
}
finally {
urlConnection.disconnect();
}
PHP:
<?PHP
$rawdata = file_get_contents("php://input");
echo $rawdata;
if(isset($_POST['Username']) && isset($_POST['PhoneNumber']))
{
$username = $_POST['Username'];
$phone_num = $_POST['PhoneNumber'];
echo "Data sent";
echo $username;
echo $phone_num;
}
else
echo "No content was sent";
?>
From my research, I used
file_get_contents("php://input")
to get any HTTP body requests. When I press the button in the app and refresh the page; nothing prints to the page. Signaling that data is lost while in transit to the server? I'm not sure whats happening, and what causes this. The android app is running on an emulator.
logcat:
07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: URL Connection enabled
07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Params [0]: Gina Params[1]: 2225550000
07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Query: Username=Gina&PhoneNumber=2225550000
07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Append Parameters to URL
07-17 20:52:54.747 2895-7142/com.example.czar.connectingtoserver D/MSG: OutPutStream connection
07-17 20:52:54.747 2895-7142/com.example.czar.connectingtoserver D/MSG: BufferedWriter established
07-17 20:52:54.748 2895-7142/com.example.czar.connectingtoserver D/MSG: Writing to Server
07-17 20:52:54.748 2895-7142/com.example.czar.connectingtoserver D/MSG: Connected
07-17 20:52:54.860 2895-2895/com.example.czar.connectingtoserver D/MSG: Successful!
07-17 20:52:54.860 2895-2895/com.example.czar.connectingtoserver D/MSG: Printed on page
This is how data is being packaged in the android app:
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("Username", params[0])
.appendQueryParameter("PhoneNumber", params[1]);
Username and PhoneNumber are the keys used, and I'm using the same keys in the php code as indexes. Not exactly sure what the problem is. Could someone shed some light as to what can cause this?