0 レビュー
1 回答
php-Androidのログインフォームにダイアログボックスが表示されないのはなぜですか?
私は、ユーザー名とパスワードを使用してユーザーを登録し、ユーザーに割り当てられたユーザーIDを使用してログインし、mysqlデータベースにレコードを送信するアプリを作成しています。レコードをデータベースに正常に保存できましたが、ログインしたいときにログインできず、次のアクティビティに移動しません。ログインボタンをクリックしても、IDまたはパスワードが正しくないというダイアログボックスが表示されない場合は、どちらも表示されません。また、ユーザーがログインするユーザーを選択する必要がある2つのラジオボタンがあります。ユーザーがログインしていない、またはダイアログボックスが表示されないのはなぜですか。データベースにラジオボタンの値を追加するにはどうすればよいですか?
これが私のコードです:
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"; }
?>
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。