JSONException:值<br不能=""be=""="="转换成=""jsonobject=""

时间:2018-07-13 18:57:39

标签: php android mysql json

="" I' m使用带有MySQL数据库的xampp服务器制作一个简单的登录页面。我不断收到上述错误,但不幸的是,找不到正确的答案。我正在使用真正的Android手机而不是模拟器。该日志框似乎没有显示任何类型的错误,因此我不确定出了什么问题。在此先感谢

LoginActivity.java

public class LoginActivity extends AppCompatActivity {

private static String LOGIN_URL = "http://192.168.43.231/Login/login.php";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";

@Override
protected void onCreate(Bundle savedInstanceState) {
    .....
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String usernameS = username.getText().toString().trim();
            final String passwordS = password.getText().toString().trim();

            if (usernameS.isEmpty() || passwordS.isEmpty()) {
                Toast.makeText(LoginActivity.this, "Enter Valid Username & Password", Toast.LENGTH_SHORT).show();
            } else {

                StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    boolean responseStatus = jsonObject.getBoolean("success");
                                    if (responseStatus) {
                                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                        LoginActivity.this.startActivity(intent);
                                    } else {
                                        Toast.makeText(LoginActivity.this, "Invalid Username or Password", Toast.LENGTH_SHORT).show();

                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    Toast.makeText(LoginActivity.this,"Error" + e.toString(), Toast.LENGTH_LONG).show();
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put(KEY_USERNAME, usernameS);
                        params.put(KEY_PASSWORD, passwordS);

                        return params;
                    }
                };
                RequestQueue requestQueue =Volley.newRequestQueue(LoginActivity.this);
                requestQueue.add(stringRequest);
            }
        }
    });
}

}

PHP代码

<?php
$con=mysqli_connect("localhost","root","","blast");

$username=$_POST['username']);
$password=$_POST['password']);

$sql=mysqli_prepare($con,"SELECT * from blast_user where username = '$username' and password = '$password'");
mysqli_stmt_bind_param($sql,"sss",$username,$password);
mysqli_stmt_execute($sql);
mysqli_stmt_store_result($sql);
mysqli_stmt_bind_result($sql,$username,$password);

$response=array();
$response["success"]=true;

echo json_encode($response);
>?

1 个答案:

答案 0 :(得分:0)

执行此操作-

$username=$_POST['usernameS']);
$password=$_POST['passwordS']);

而不是-

$username=$_POST['username']);
$password=$_POST['password']);