0

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 :

 <?php 
$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."<br>".$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.

1 Answers1

1

openConnection

It should be noted that a URLConnection instance does not establish the actual network connection on creation. This will happen only when calling URLConnection.connect().

You need to call urlConnection.connect() to execute your call

URL url = new URL("https://smssaver.000webhostapp.com/helloworld/insert_data.php?sms_content=100");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// additional attributes for request for efficiency 
urlConnection.setReadTimeout(1000);
urlConnection.setConnectTimeout(1000);
urlConnection.setRequestMethod("GET");

urlConnection.connect();
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • 1
    For a GET request, it should be `setDoOutput(false)` – Daniel Nugent Sep 12 '17 at 18:23
  • Thank you for your response. The application suddenly stops once I click the button and it is still not able to insert the data into the database. – Shiva Bhusal Sep 12 '17 at 18:30
  • have you mentioned the internet permissions in manifest please verify it and also post the logcat error details – Pavneet_Singh Sep 12 '17 at 18:31
  • 1
    Thank you again. I have set internet permission in my manifest as . – Shiva Bhusal Sep 12 '17 at 18:39
  • read this https://developer.android.com/studio/debug/am-logcat.html and post the result of error filer further edit your post to add more details – Pavneet_Singh Sep 12 '17 at 18:44
  • Thanks ! The error is : java.lang.SecurityException: Permission denial: writing to settings requires android.permission.WRITE_SETTINGS at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:730) – Shiva Bhusal Sep 12 '17 at 18:50
  • FATAL EXCEPTION: main android.os.NetworkOnMainThreadException – Shiva Bhusal Sep 12 '17 at 18:50
  • read and implement this https://stackoverflow.com/questions/15496278/httpurlconnection-is-throwing-exception and it's happening because you can't executed network calls on main thread – Pavneet_Singh Sep 12 '17 at 18:57