Почему диалоговое окно не появляется в форме входа в Android?

Я делаю приложение, которое просто регистрирует пользователя с помощью имени пользователя и пароля, и входит в систему с помощью идентификатора пользователя, которое назначается пользователю и отправляет запись в базу данных mysql. Я успешно могу сохранить запись в базе данных, но когда я хочу войти, я не могу этого сделать, она даже не переходит к следующему действию. Когда я нажимаю на кнопку входа в систему, и в диалоговом окне не отображается, что идентификатор или пароль неверны, это не делает ни один из них. У меня также есть две радиокнопки, в которых пользователь должен выбрать, в кого он входит. Почему пользователь не входит в систему или показывает диалоговое окно? Как я могу добавить значение переключателя в базе данных?

Вот мой код:

background.java

public class backgroundTask extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;

backgroundTask(Context ctx) {
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login successful");
}

@Override
protected String doInBackground(String... params) {
String reg_url = "http://192.168.10.9/app/register.php";
String login_url = "http://192.168.10.9/app/login.php";
String method = params[0];
if (method.equals("register")) {
String username = params[1];
String password = params[2];
String phone_no = params[3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
//httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
URLEncoder.encode("phone_no", "UTF-8") + "=" + URLEncoder.encode(phone_no, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
//httpURLConnection.connect();
httpURLConnection.disconnect();
return "Registration Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("login"))
{
String id = params[1];
String password = params[2];
try {
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 data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
if(result.equals("Registration Successful"))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else
{
alertDialog.setMessage(result);
alertDialog.show();
}
}

}

это login.java:

public class login extends AppCompatActivity implements View.OnClickListener{

Button bLogin;
TextView registerLink;
EditText  etUserId, etPassword;
String id, password;
RadioButton  rdParent, rdChild;
RadioGroup rg;

@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);etUserId = (EditText) findViewById(R.id.etUserId);
etPassword = (EditText) findViewById(R.id.etPassword);
registerLink = (TextView) findViewById(R.id.registerLink);registerLink.setOnClickListener(this);
// selectItem();

}

Я прокомментировал переключатели, потому что подумал, что эти переключатели не позволяют войти в систему:

 /* public void selectItem()
{

rg = (RadioGroup) findViewById(R.id.rg);
rdParent = (RadioButton) findViewById(R.id.rdParent);
rdChild = (RadioButton) findViewById(R.id.rdChild);
bLogin = (Button) findViewById(R.id.bLogin);
bLogin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if ( rg.getCheckedRadioButtonId() == R.id.rdParent)
{
Intent rdParentIntent = new Intent(login.this,synchronization.class);
startActivity(rdParentIntent );
}
else if(rg.getCheckedRadioButtonId() == R.id.rdChild)
{
Intent rdChildIntent = new Intent(login.this,emergency.class);
startActivity(rdChildIntent);
}
}
});

} */
@Override
public void onClick(View view) {

switch (view.getId()) {
case R.id.registerLink:
Intent registerIntent = new Intent(login.this,register.class);
startActivity(registerIntent);
break;
case R.id.bLogin:
id = etUserId.getText().toString();
password = etPassword.getText().toString();
String method = "login";
backgroundTask bTask = new backgroundTask(this);
bTask.execute(method, id, password);
break;
}
}
}

login.php:

<?php
require"db_connect.php";
$id = $_POST["id"];
$password = $_POST["password"];
$query = "select * from user where id like '$id' and password like '$password'";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result)>0) {
echo "login successful";
}
else{
echo "incorrect id or password";
}

?>

-1

Решение

Опробовал ваш код и работает так, как вы хотели для меня.
Так что вы можете попробовать ниже

@Override
protected void onPostExecute(String s) {
AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login successful");
alertDialog.setMessage(s);
alertDialog.show();
}

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector