简体   繁体   English

字符串无法转换为JSONObject-Android

[英]String cannot be converted to JSONObject - Android

Ok, guys. 好了朋友们。 I developed a Login-Registration System using MySQL and PHP. 我使用MySQL和PHP开发了一个登录注册系统。 At the beginning, it worked perfectly, being able to register new accounts, and, obviously, to LogIn. 刚开始时,它运行良好,能够注册新帐户,并且显然可以登录。 But, for 2 days, I'm receiving some weird errors(?!) in my Android. 但是,在两天内,我的Android设备出现了一些奇怪的错误(?!)。

This is my PHP code: 这是我的PHP代码:

<?php
$con = mysqli_connect("***", "***", "***", "***");

$email = $_POST["email"];
$password = $_POST["password"];
$name = $_POST["name"];
$age = $_POST["age"];
$location = $_POST["location"];


$statement = mysqli_prepare($con, "INSERT INTO useraccounts (email, password, name, age, location) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssis", $email, $password, $name, $age, $location);
mysqli_stmt_execute($statement);

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

echo json_encode($response);

This is my RegisterRequest class: 这是我的RegisterRequest类:

public class RegisterRequest extends StringRequest {

private static final String REGISTER_REQUESTURL = "http://docscanner.ezyro.com/Register.php";
private Map<String, String> params;

public RegisterRequest(String email, String password, String name, int age, String location, Response.Listener<String> listener){
    super(Method.POST, REGISTER_REQUESTURL, listener, null);
    params = new HashMap<>();
    params.put("email", email);
    params.put("password", password);
    params.put("name", name);
    params.put("age", age+ "");
    params.put("location", location);
}

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

And, finally, this is my method: 最后,这是我的方法:

    try {
        final EditText etEmail = (EditText) findViewById(R.id.emailTxt);
        final EditText etPassword = (EditText) findViewById(R.id.passwordTxt);
        final EditText etName = (EditText) findViewById(R.id.nameTxt);
        final EditText etAge = (EditText) findViewById(R.id.ageTxt);
        final EditText etLocation = (EditText) findViewById(R.id.locationTxt);

        final String email = etEmail.getText().toString();
        final String password = etPassword.getText().toString();
        String name = etName.getText().toString();
        int age = Integer.parseInt(etAge.getText().toString());
        String location = etLocation.getText().toString();

        if (!etEmail.equals("") && !etPassword.equals("") && !etName.equals("") && !etAge.equals("") && !etLocation.equals("")) {
            if (isValidEmailAddress(email)) {

                Response.Listener<String> listener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            boolean success = jsonObject.getBoolean("success");
                            if (success) {
                                Intent loginIntent = new Intent(getApplicationContext(), LoginActivity.class);
                                loginIntent.putExtra("emailExtra", email);
                                loginIntent.putExtra("passwordExtra", password);
                                startActivity(loginIntent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Register failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(email, password, name, age, location, listener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            } else {
                Toast.makeText(getApplicationContext(),
                        "Email format is not valid",
                        Toast.LENGTH_LONG)
                        .show();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "All fields must be completed",
                    Toast.LENGTH_LONG)
                    .show();
        }
    }catch(NumberFormatException nEx){
        Toast.makeText(getApplicationContext(),
                "Please complete all fields in order to submit the document",
                Toast.LENGTH_LONG)
                .show();
    }
}

My error: 我的错误:

W/System.err: org.json.JSONException: Value W / System.err:org.json.JSONException:值

There is problem with your php code as far as i can see. 据我所知,您的php代码存在问题。 Add this to your php file (in the start) 将此添加到您的php文件(一开始)

header('Content-Type: application/json');

and another problem I saw was that when making a simple post request (FROM POSTMAN) it returns the following data :- 我看到的另一个问题是,在进行简单的发布请求(FROM POSTMAN)时,它会返回以下数据:

<html>
    <body>
        <script type="text/javascript" src="/aes.js" ></script>
        <script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f
            <d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("3c1ab4cc426e0aacb5f07f248a1799fe");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://docscanner.ezyro.com/Register.php?i=2";
            </script>
            <noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>
        </body>
    </html>

The following is obviously not a string containing JSON data. 以下显然不是包含JSON数据的字符串。 try adding the header part of the code specified and if possible you should think about moving to Firebase auth or some other auth service becuase you are sending the non- encrypted password over an HTTP connection. 尝试添加指定代码的标头部分,如果可能的话,您应该考虑转移到Firebase身份验证或其他身份验证服务,因为您正在通过HTTP连接发送非加密密码。 You are also sending an non-hashed password to your db which is another problem. 您还向数据库发送了一个未加密的密码,这是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM