I’m very new to android programming and I have a problem with OutputStreamWriter. Now, I’m working on a simple test application to send POST requests to a server. There is a PHP application on the server-side, which is designed to write data in a MYSQL database. When I run my android app it accesses the PHP file correctly but the parameters are not passed.
I have been conductive an extensive search over all types of forums and tried some of the answers given, but nothing worked for me. What am I doing wrong?
PHP
<?$conecta = mysqli_connect('localhost','username','password','killerbees');
if (!$conecta) {echo "Unable to verify nickname. Please, try again later!";}
else
{
$sql = "SELECT * FROM KillerBees WHERE nickname = '" . addslashes($_POST['kbfnickname']) . "'";
if ($resultado = mysqli_query($conecta,$sql))
{
if (mysqli_num_rows($resultado) == 0)
{
$sql = "INSERT INTO KillerBees (nickname, password, level, seconds) VALUES ('" . addslashes($_POST['kbfnickname']) . "', '" . addslashes($_POST['kbfpassword']) . "', 0, 0)";
$resultado = mysqli_query($conecta,$sql);
$_SESSION['nickname'] = $_POST['kbfnickname'];
$_SESSION['level'] = 0;
$_SESSION['seconds'] = 0;
echo "form_register";
}
else
{
$linha = mysqli_fetch_array($resultado);
if ($linha['password']==stripslashes($_POST['kbfpassword']))
{
$file = glob("users/".$_POST['kbfnickname']."*");
$_SESSION['nickname'] = $_POST['kbfnickname'];
$_SESSION['level'] = $linha['level'];
$_SESSION['seconds'] = $linha['seconds'];
if (sizeof($file)!=0 and $linha['email']!="")
{
echo "form_game";
}
else
{
echo "form_register";
}
}
else
{
echo "The combination nickname/password doesn't exist in our database.";
}
}
}
else
{
echo "Unable to verify nickname. Please, try again later!";
}
if ($conecta) mysqli_close($conecta);
}?>
ANDROID JAVA
@Override
protected String doInBackground(String... arg0) {
try{
String nickname = arg0[0];
String password = arg0[1];
String link="https://savvyartstudio.com/killerbees/signin.php";
String urlParameters = URLEncoder.encode("kbfnickname", "UTF-8") + "=" + URLEncoder.encode(nickname, "UTF-8");
urlParameters += "&" + URLEncoder.encode("kbfpassword", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(urlParameters);
wr.flush();
wr.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
line = reader.readLine();
sb.append(line);
reader.close();
return sb.toString();
} catch(Exception e){
return "Unable to verify nickname. Please, try again later!";
}
}
Any help would be appreciated!
guys
Thank you all for your help. After two days of trying to solve my problem, I was only able to solve it by changing to the $_GET method; the $_POST method didn’t work for me, at all.
I used the $_GET method explained here. https://gist.github.com/hitenpratap/8e1f28d60c0bb11a5bca