登录模块不起作用

时间:2018-01-21 14:27:52

标签: java android-studio android-intent

我正在开发一个Android应用程序,帮助: https://www.youtube.com/watch?v=T7Z4GVFaT4A&list=PLe60o7ed8E-TztoF2K3y4VdDgT6APZ0ka&index=4

当我回复相同的教程时,应用程序工作得很好。然后我调整了应用程序以适应我的应用程序想法。注册成功并按预期将数据存储到数据库中。 但是在LoginActivity中面临问题。

应用程序应该继续的预期方式:当我单击blogin按钮时,它应该验证存储在db中的用户并打开相应的AdminViewActivity。

实际发生的是当我点击按钮blogin时,没有任何反应。表名是“allusers”,已包含值。我通过我的应用程序登录时输入相同的值。

请帮我找出错误的地方。提前谢谢。

LoginActivity.java

package com.example.android.ablo;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

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

        final Button bregister = (Button) findViewById(R.id.bregister);
        final Button blogin = (Button) findViewById(R.id.blogin);
        final EditText etusername=(EditText)findViewById(R.id.etusername);
        final EditText etpassword=(EditText)findViewById(R.id.etpassword);


        bregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
                LoginActivity.this.startActivity(i);
            }
        });



       blogin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                final String username=etusername.getText().toString();
                final String password=etpassword.getText().toString();

                Response.Listener<String> responseListener= new Response.Listener<String>(){
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success= jsonResponse.getBoolean("success");

                            if(success){
                                String name= jsonResponse.getString("name");
                                Intent i=new Intent(LoginActivity.this,AdminViewActivity.class);

                                i.putExtra("name",name);

                                LoginActivity.this.startActivity(i);


                            }else{
                                AlertDialog.Builder builder= new AlertDialog.Builder(LoginActivity.this);
                                builder.setMessage("Login Failed").setNegativeButton("Retry",null).create().show();

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                LoginRequest loginRequest=new LoginRequest(username,password,responseListener );
                RequestQueue queue= Volley.newRequestQueue(LoginActivity.this);
                queue.add(loginRequest);

            }
        });

    }
}

LoginRequest.java

package com.example.android.ablo;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by User on 21-01-2018.
 */

public class LoginRequest extends StringRequest {
    private static  final String LOGIN_REQUEST_URL = "http://ablo.000webhostapp.com/AbloLogin.php";
    private Map<String,String> params;

    public LoginRequest( String username, String password, Response.Listener<String> listener){
        super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null );
        params= new HashMap<>();

        params.put("username",username);

        params.put("password",password);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

AbloLogin.php

<?php

    $con = mysqli_connect("localhost", "id4335430_ablo_users", "wontreveal", "id4335430_ablo_users");



    $username = $_POST["username"];

    $password = $_POST["password"];



    $statement = mysqli_prepare($con, "SELECT * FROM allusers WHERE username = ? AND password = ?");

    mysqli_stmt_bind_param($statement, "ss", $username, $password);

    mysqli_stmt_execute($statement);



    mysqli_stmt_store_result($statement);

    mysqli_stmt_bind_result($statement, $allusersID, $name,  $username, $password);



    $response = array();

    $response["success"] = false;  



    while(mysqli_stmt_fetch($statement)){

        $response["success"] = true;  

        $response["name"] = $name;

        $response["username"] = $username;

        $response["password"] = $password;

    }



    echo json_encode($response);

?>

AdminViewActivity.java

package com.example.android.ablo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class AdminViewActivity extends AppCompatActivity {

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

        final Button badduser = (Button) findViewById(R.id.badduser);
        final Button bmanageusers = (Button) findViewById(R.id.bmanageusers);

        final TextView tvadmin =(TextView) findViewById(R.id.tvadmin);

        Intent i= getIntent();
        String name= i.getStringExtra("name");

        String message= name+ "'s Admin View";
        tvadmin.setText(message);

        badduser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(AdminViewActivity.this, CreateUserActivity.class);
                AdminViewActivity.this.startActivity(i);
            }
        });

        bmanageusers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(AdminViewActivity.this, ManageUsersActivity.class);
                AdminViewActivity.this.startActivity(i);
            }
        });

    }
}

0 个答案:

没有答案