字符串无法转换为JSONObject - Android

时间:2016-11-03 10:11:28

标签: php android mysql

好的,伙计们。我使用MySQL和PHP开发了一个登录注册系统。最初,它完美地工作,能够注册新帐户,显然,登录到LogIn。但是,2天后,我在Android中收到了一些奇怪的错误(?!)。

这是我的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);

这是我的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;
}

最后,这是我的方法:

    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();
    }
}

我的错误:

  

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

1 个答案:

答案 0 :(得分:0)

据我所知,您的PHP代码存在问题。将此添加到您的php文件(在开始)

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

我看到的另一个问题是,当发出简单的帖子请求(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>

以下显然不是包含JSON数据的字符串。尝试添加指定代码的标题部分,如果可能,您应该考虑转移到Firebase身份验证或其他一些身份验证服务,因为您通过HTTP连接发送非加密密码。您还向数据库发送了一个非散列密码,这是另一个问题。