简体   繁体   English

JSONException字符串无法转换为JSONArray

[英]JSONException String cannot be converted to JSONArray

I have been trying to make a login system for my app in android studio and I keep getting this error 我一直在尝试为Android Studio中的应用制作一个登录系统,但不断出现此错误

org.json.JSONException: Value Connection of type java.lang.String cannot be converted to JSONArray org.json.JSONException:类型为java.lang.String的值连接无法转换为JSONArray

and could not fix it in any way, I hope you can spot it and help me out. 并且无法以任何方式修复它,希望您能发现并帮助我。 Here is a part of the login activity where I believe the problem is (I can provide more code if needed) 我认为这是登录活动的一部分(如果需要,我可以提供更多代码)

    {StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try
                {
                    JSONArray jsonArray = new JSONArray(response);
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    String code = jsonObject.getString("code");
                    if(code.equals("login_failed"))
                    {
                        builder.setTitle("Login Error...");
                        displayAlert(jsonObject.getString("message"));
                    }
                    else
                    {
                        Intent intent = new Intent(LoginActivity.this,TestActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("email",jsonObject.getString(("email")));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error)
    {
        Toast.makeText(LoginActivity.this,"Error",Toast.LENGTH_LONG).show();
        error.printStackTrace();
    }
})
{
    @Override
    protected Map<String, String> getParams() throws AuthFailureError
    {
        Map<String,String> params = new HashMap<String, String>();
        params.put("email",email);
        params.put("password",password);
        return params;
    }
};
    MySingleton.getmInstance(LoginActivity.this).addToRequestQue(stringRequest);

}

here is the php code: 这是PHP代码:

<?php
require "connection.php";
$email = $_POST["email"];
$password = $_POST["password"];

$sql = "select email,password from user_info where email like '".$email."' and password like '".$password."';";
$result = mysqli_query($connection,$sql);
$response = array();
if(mysqli_num_rows($result)>0)
{
    $row = mysqli_fetch_row($result);
    $email = $row[0];
    $password = $row[1];
    $code = "login_success";
    array_push($response,array("code"=>$code,"email"=>$email,"password"=>$password));
    echo json_encode($response);
}
else
{
    $code = "login_failed";
    $message = "User not found...";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode($response);    
}
mysqli_close($connection);
?>

You can't create a JSONArray from a String unless that string is a proper stringified JSON file. 除非该字符串是正确的字符串化JSON文件,否则无法从String创建JSONArray。

You have to, in your PHP, convert your array into a JSON string instead. 您必须在PHP中将数组转换为JSON字符串。 (or vice versa if I understood the relationship incorrectly, I didn't read your code too accurately). (反之亦然,如果我对关系的理解不正确,则可能是因为我阅读的代码不太准确)。

Another solution is for you to use jquery and an ajax function to call the PHP script (I dont' know what you're creating, could be a Cordova plugin in which case you have Javascript, otherwise you'll have to do something similar in Java, google is your friend). 另一个解决方案是让您使用jquery和ajax函数来调用PHP脚本(我不知道您要创建的内容,可能是Cordova插件,在这种情况下您具有Javascript,否则您将必须执行类似的操作Java,Google是您的朋友)。

Here's a function taken straight from one of my own apps I'm making. 这是直接从我自己开发的一个应用程序中提取的功能。 Read through it, it might not be 100% copy pastable. 通读它,它可能不是100%复制可粘贴的。 The important parts are that it converts a javascript object into a proper JSON string before sending it to your PHP function. 重要的部分是,它将JavaScript对象转换为适当的JSON字符串,然后再将其发送到PHP函数。

function login(successCallback, errorCallback, args) {
var loginData = {
    "username": args[0].username,
    "password": args[0].password
};
var dataString = JSON.stringify(loginData);
$.ajax({
    url: 'login.php',
    method: 'post',
    data: {loginData: dataString},
    dataType: 'json',
    complete: function (data) {
        console.log(data.responseText);
    },
    error: function (xhr, status, errorThrown) {
        console.log("Error: " + errorThrown);
        console.log("Status: " + status);
        errorCallback('Error logging in!');
    },
    success: function (data) {
        if (data.status == "success") {
            console.log(data.responseText);
            successCallback('Success logging in!');
        } else if (data.status = "noConnection") {
            console.log(data.responseText);
            errorCallback('Could not connect to database.');
        } else {
            console.log(data.responseText);
            errorCallback('Error logging in (but database was connected to)!');
        }
    }
});

} }

暂无
暂无

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

相关问题 JSONException:无法将类型为java.lang.String的值转换为JSONArray - JSONException: Value of type java.lang.String cannot be converted to JSONArray JSONException:无法将类型为java.lang.String的jsonData转换为JSONArray - JSONException : at jsonData of type java.lang.String cannot be converted to JSONArray JSONException:值数组无法转换为JSONArray - JSONException: Value Array cannot be converted to JSONArray JSONException:值 - JSONException: Value <br of type java.lang.String cannot be converted to JSONArray org.json.JSONException:类型为java.lang.String成功时的值0无法转换为JSONArray - org.json.JSONException: Value 0 at success of type java.lang.String cannot be converted to JSONArray 解析类型为java.lang.String的data.org.json.JSONException值时出错,无法转换为JSONArray - Error parsing data.org.json.JSONException value of type java.lang.String cannot be converted to JSONArray org.json.JSONException值java.lang.String类型的连接无法转换为JSONArray - org.json.JSONException value Connection of type java.lang.String cannot be converted to JSONArray 解析数据org.json.JSONException时出错:类型java.lang.String的值无法转换为JSONArray - Error Parsing Data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray JSONException 字符串无法转换为 JSONObject - JSONException String cannot be converted to JSONObject 字符串的值不能转换为JSONArray - Value of string cannot be converted to JSONArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM