Login via PHP and Mysql in Android
I am working on an android application that requires user login for the following processes. In my application it is the same as username and password. Through PHP, it checks the requirements and gives success or failure as a toast. But after the next success, it does not proceed to the next step, because the string variable (s) in onPostExecute
does not receive any value (success or failure) from the PHP script. Please provide me with a solution for the above.
My Java code:
public class login extends AppCompatActivity implements View.OnClickListener{
public static final String USER_NAME = "USER_NAME";
public static final String PASSWORD = "PASSWORD";
private static final String LOGIN_URL = "http://knitstudents.dx.am/login11.php";
private EditText editTextUserName;
private EditText editTextPassword;
Button buttonRegister;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
editTextUserName = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
buttonLogin = (Button) findViewById(R.id.login);
buttonLogin.setOnClickListener(this);
buttonRegister= (Button) findViewById(R.id.buttonRegister);
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myintent2 = new Intent(login.this,register.class);
startActivity(myintent2);
}
});
}
private void userlogin(){
String username = editTextUserName.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
userLogin(username,password);
}
private void userLogin(final String username, final String password){
class UserLoginClass extends AsyncTask<String,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(login.this,"Please Wait",null,true,true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.equalsIgnoreCase("success")){
Intent intent = new Intent(login.this,Welcome.class);
intent.putExtra(USER_NAME,username);
startActivity(intent);
}else{
Toast.makeText(login.this,s,Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("username",params[0]);
data.put("password",params[1]);
RegisterUserClass ruc = new RegisterUserClass();
String result = ruc.sendPostRequest(LOGIN_URL,data);
return result;
}
}
UserLoginClass ulc = new UserLoginClass();
ulc.execute(username,password);
}
@Override
public void onClick(View v) {
if(v == buttonLogin){
userlogin();
}
}
}
login11.php -
<?php
require "index111.php";
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from users_information where username='$username' and password='$password'";
$res = mysqli_query($conn,$sql);
$check = mysqli_fetch_array($res);
if(isset($check)){
echo 'success';
}else{
echo 'failure';
}
mysqli_close($conn);
?>
RegisterUserClass (sendPostRequest and getPostDataString):
public class RegisterUserClass {
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
+3
source to share