Thursday, April 25, 2019

php - Passing a get request to remote server in android

I am trying to send a post request to a remote server from an android application. The request should insert values in one of the columns of the database. The PHP code on the server side is like this :



 $servername="sql9.freesqldatabase.com"; 
$username="sql9193859";
$password="MUEpGxKWUt";
$dbname="sql9193859";

$sms_content=$_GET["sms_content"];

$conn=new mysqli($servername,$username,$password,$dbname);

// check connection

if ($conn->connect_error){
die("Connection failed: ".$conn->connect_error);
}
else {

echo "connected successfully";
$sql="insert into SMS_TABLE(content) values ($sms_content)";
if ($conn->query($sql)===true){
echo "New record inserted successfully";
}
else {
echo "Error:".$sql."
".$conn->error;
}
}


?>


The method to send a get request to the remote server is like this :



public void insertSMS() throws IOException {
viewbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {

URL url = new URL("https://smssaver.000webhostapp.com/helloworld/insert_data.php?sms_content=100");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.disconnect();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});



}


When I type the link on the browser, it is working properly. But, it's not working when I call it in Android.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...