Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php连接和mysql连接中单击登录按钮时应用程序崩溃_Php_Android_Mysql_Android Studio - Fatal编程技术网

在php连接和mysql连接中单击登录按钮时应用程序崩溃

在php连接和mysql连接中单击登录按钮时应用程序崩溃,php,android,mysql,android-studio,Php,Android,Mysql,Android Studio,我正在尝试创建一个登录活动,在这里我验证并检查值是否正确且不为空。请帮助我。。。在这里,我用以下代码单击登录按钮,我的应用程序崩溃。请先帮我谢谢。 我是android studio编程新手,请耐心等待,谢谢 LoginActivity.java package com.map.laurence.dbtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.uti

我正在尝试创建一个登录活动,在这里我验证并检查值是否正确且不为空。请帮助我。。。在这里,我用以下代码单击登录按钮,我的应用程序崩溃。请先帮我谢谢。 我是android studio编程新手,请耐心等待,谢谢

LoginActivity.java

package com.map.laurence.dbtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;

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

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

public class Login extends AppCompatActivity {

    EditText user,pass;
    Button login,register;

    String loginUrl = "http://192.168.56.1/login.php";
    RequestQueue requestQueue;


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


        register = (Button) findViewById(R.id.btnRegister);
        login = (Button) findViewById(R.id.btnLogin);
        user = (EditText) findViewById(R.id.user);
        pass = (EditText) findViewById(R.id.pass);


        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    Toast.makeText(Login.this, "You clicked insert", Toast.LENGTH_LONG).show();
                    StringRequest request = new StringRequest(Request.Method.POST, loginUrl, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            System.out.println(response.toString());
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }) {

                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> parameters = new HashMap<String, String>();
                            parameters.put("user", user.getText().toString());

                            return parameters;
                        }
                    };
                    requestQueue.add(request);


                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                            loginUrl, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            System.out.println(response.toString());
                            Toast.makeText(Login.this, "You clicked Login", Toast.LENGTH_LONG).show();
                            try {
                                JSONArray students = response.getJSONArray("user");

                                for (int i = 0; i < students.length(); i++) {

                                    JSONObject student = students.getJSONObject(i);

                                    String user = student.getString("username");
                                    String password = student.getString("password");
                                    String access = student.getString("access");
                                    if (user != null) {
                                        if (password == pass.toString()) {
                                            Toast.makeText(Login.this, "not null", Toast.LENGTH_LONG).show();
                                        } else {
                                            Toast.makeText(Login.this, "password incorrect", Toast.LENGTH_LONG).show();
                                        }


                                    } else {
                                        Toast.makeText(Login.this, "its null", Toast.LENGTH_LONG).show();
                                    }


                                }


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            System.out.append(error.getMessage());

                        }
                    });
                    requestQueue.add(jsonObjectRequest);

            }


        });

    }

}
package com.map.laurence.dbtest;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入com.android.volley.AuthFailureError;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.StringRequest;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.HashMap;
导入java.util.Map;
公共类登录扩展了AppCompatActivity{
编辑文本用户,通过;
按钮登录、注册;
字符串loginUrl=”http://192.168.56.1/login.php";
请求队列请求队列;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
寄存器=(按钮)findViewById(R.id.btnRegister);
登录=(按钮)findViewById(R.id.btnLogin);
user=(EditText)findViewById(R.id.user);
pass=(EditText)findViewById(R.id.pass);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Toast.makeText(Login.this,“您单击了插入”,Toast.LENGTH_LONG.show();
StringRequest=newStringRequest(request.Method.POST、loginUrl、new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
System.out.println(response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}) {
@凌驾
受保护的映射getParams()引发AuthFailureError{
映射参数=新的HashMap();
parameters.put(“user”,user.getText().toString());
返回参数;
}
};
添加(请求);
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,
loginUrl,新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
System.out.println(response.toString());
Toast.makeText(Login.this,“您点击了登录”,Toast.LENGTH_LONG.show();
试一试{
JSONArray students=response.getJSONArray(“用户”);
for(int i=0;i
布局文件login.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.map.laurence.dbtest.Login">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="50sp"
                android:id="@+id/user"
                android:hint="Username"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50sp"
                android:id="@+id/pass"
                android:hint="Password"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/btnLogin"
                android:text="LOGIN"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/btnRegister"
                android:text="REGISTER"/>

        </LinearLayout>

    </RelativeLayout>

login.php

<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
    include 'connection.php';
    loginStudent();

}

function loginStudent()
{
    $user = $_POST["user"];
    $pass = $_POST["pass"];


    global $connect;
    $query = "select * from user where username='$user';";
    $result = mysqli_query($connect,$query);
    $number_of_rows = mysqli_num_rows($result);

    $temp_array = array();

    if($number_of_rows > 0 ){
        while($row = mysqli_fetch_assoc($result)){
        $temp_array[] = $row;

        }
}

header('Content-Type: application/json');
echo json_encode(array("students"=>$temp_array));
mysqli_close($connect);
}
?>

此行负责NullPointerException。
您正在对空对象调用requestQueue.add(request)方法

首先初始化requestQueue对象,如下面的示例代码所示

RequestQueue requestQueue = VolleySingleton.getInstance(this).getRequestQueue();

把你的Logcat放进去。刚刚用错误siri更新了它。我想你忘了初始化requestQueue参数。我应该把它当作字符串值吗?
RequestQueue requestQueue = VolleySingleton.getInstance(this).getRequestQueue();