I try to learn android programming on the internet but when I want to create a login system it just doesn't write "Succes". My php file:
<?php
$con = mysqli_connect("hostname", "username", "password", "database");
$email = $_POST['email'];
$password = $_POST['password'];
$query = mysqli_query($con, "SELECT id FROM users WHERE email like '$email' and password like '$password'");
if(mysqli_num_rows($query) > 0){
echo "Success!";
} else {
echo "Wrong e-mail or password!";
}
mysqli_close($con);
?>
and I also got 2 java class, the first is the MainActivity:
public class MainActivity extends AppCompatActivity {
EditText email, password;
//Button loginbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email = (EditText) findViewById(R.id.editText2);
password = (EditText) findViewById(R.id.editText3);
}
public void onLogin(View view){
String Email = email.getText().toString();
String Password = password.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(Email, Password, type);
}
}
and the second is my backgroundWorker:
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context cdx){
context = cdx;
}
@Override
protected String doInBackground(String... params){
String type = params[0];
String login_url = "http://diamondearth.16mb.com/android/login.php";
if(type.equals("login")){
try {
String email = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute(){
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result){
alertDialog.setMessage(result);
alertDialog.show();
}
}