I'm trying to insert data from my app to the MYSQL database. Im using `HttpURLConnection` to open a connection and writing it with a `BufferedWriter`. Everything works (nothing catches any exception) but somehow no data is being inserted into the table. (Im following [this](https://www.youtube.com/watch?v=cOsZHuu8Qog) tutorial).

I'm using the `AsyncTask` to do the networking in the background. As shown below I've created a new class who's extending from `AsyncTask`.

```
public class BackgroundTask extends AsyncTask<String, Void, String>
{
    Context ctx;
    BackgroundTask(Context ctx)
    {
        this.ctx = ctx;
    }

```

This is the doInBackground method.

```
@Override
protected String doInBackground(String... strings)
{
    String registerUrl = "http://localhost/APPoint/register.php";
    String method = strings[0];
    if (method.equals("register"))
    {
        try
        {
            String gebruikersnaam = strings[1];
            String wachtwoord = strings[2];
            String voornaam = strings[3];
            String tussenvoegsel = strings[4];
            String achternaam = strings[5];
            String bedrijfsnaam = strings[6];
            String email = strings[7];
            String telefoonnummer = strings[8];
            URL url = new URL(registerUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStream OS = conn.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(OS));
            String data = URLEncoder.encode("Gebruikersnaam", "UTF-8")+ "=" + URLEncoder.encode(gebruikersnaam, "UTF-8")+"&"+
                    URLEncoder.encode("wachtwoord", "UTF-8")+ "=" + URLEncoder.encode(wachtwoord, "UTF-8")+"&"+
                    URLEncoder.encode("voornaam", "UTF-8")+ "=" + URLEncoder.encode(voornaam, "UTF-8")+"&"+
                    URLEncoder.encode("tussenvoegsel", "UTF-8")+ "=" + URLEncoder.encode(tussenvoegsel, "UTF-8")+"&"+
                    URLEncoder.encode("achternaam", "UTF-8")+ "=" + URLEncoder.encode(achternaam, "UTF-8")+"&"+
                    URLEncoder.encode("bedrijfsnaam", "UTF-8")+ "=" + URLEncoder.encode(bedrijfsnaam, "UTF-8")+"&"+
                    URLEncoder.encode("email", "UTF-8")+ "=" + URLEncoder.encode(email, "UTF-8")+"&"+
                    URLEncoder.encode("telefoonnummer", "UTF-8")+ "=" + URLEncoder.encode(telefoonnummer, "UTF-8");
            try {
                bw.write(data);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            bw.flush();
            InputStream inputS = conn.getInputStream();
            inputS.close();
            bw.close();
            OS.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Registreren voltooid";
    }
    return null;
}

```

My php DB connection initializer.

```
<?php

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', 'appoint');

$conn = new PDO('mysql:dbname=' . DB . ';host=' . HOST, USER, PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

```

My register.php file

```
<?php
$username = filter_var(trim($_POST['gebruikersnaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$password = filter_var(trim($_POST['wachtwoord']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$voornaam = filter_var(trim($_POST['voornaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$tussenvoegsel = filter_var(trim($_POST['tussenvoegsel']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$achternaam = filter_var(trim($_POST['achternaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$bedrijfsnaam = filter_var(trim($_POST['bedrijfsnaam']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$telefoonnummer = filter_var(trim($_POST['telefoonnummer']), FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

if ($username == '' || $password == '' || $voornaam == '' || $tussenvoegsel == '' || $achternaam == '' || $bedrijfsnaam == '' || $email == '' || $telefoonnummer == '') {
    //echo 'Verplichte velden zijn niet ingevoerd!';
} else {
    require_once('DBConnect.php');

    $stmnt = $conn->prepare("INSERT INTO gebruikers (`gebruikersnaam`, `wachtwoord`, `voornaam`, `tussenvoegsel`, `achternaam`, `bedrijfsnaam`, `email`, `telefoonnummer`) "
            . "VALUES (?, ? , ? , ?, ?, ?, ?, ?)");
    try {
        $stmnt->execute(array($username, $password, $voornaam, $tussenvoegsel, $achternaam, $bedrijfsnaam, $email, $telefoonnummer));
    } catch (PDOException $ex) {
        if ($ex->errorInfo[1] == 1062) {
            echo 'duplicate entry';
        } else {
            var_dump($ex);
        }
    }
}

```

At first I thought my URL was wrong. I've tried using:

```
"http://127.0.0.1/APPoint/register.php"
"http://localhost/APPoint/register.php"
"http://10.0.2.2/APPoint/register.php"
"http://192.168.0.1/APPoint/register.php"

```

Nothing seems to do the job. If anyone could have some input would be appreciated.

**Edit** I should've mentioned that i'm not using an AVD to test the code. Im using my own samsung device for this.

**Edit** adding the execute for backgroundtask

```
if(!errors)
        {
            String method = "register";
            BackgroundTask bgTask = new BackgroundTask(this);
            bgTask.execute(method, gebruikersnaam, wachtwoord, voornaam, tussenvoegsel, achternaam, bedrijfsnaam, email, telefoonnummer);
            finish();
        }

```

**Edit** On logcat this error shows up while running with AVD (probably the android bug): 
getSlotFromBufferLocked: unknown buffer 0xb4095170 
After a google search it looks like an some android error regarding the permissions. But i already have a internet permission in my manifest. 

```
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

```