【问题标题】:Android Programming : Value of type java.lang.String cannot be converted to JSONObjectAndroid 编程:java.lang.String 类型的值无法转换为 JSONObject
【发布时间】:2017-11-23 23:19:44
【问题描述】:

我正在尝试从连接数据库的 Android 活动创建登录页面。我收到此错误“org.json.JSONException:值

我是 Android 编程新手,我尝试了三天。谢谢。

这是活动代码

private void Validation() {
    strEmail = etEmail.getText().toString().trim();
    strPassword = etPass.getText().toString().trim();
    class validate extends AsyncTask<String, String, JSONObject> {
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading  = ProgressDialog.show(SignIn.this,"Progressing...","Wait...",false,false);
        }

        @Override
        protected JSONObject doInBackground(String... params) {
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.userValidation(strEmail,strPassword);
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
            loading.dismiss();
            try {
                String successID = jsonObject.getString("success");
                //  String userID = jsonObject.getString("userID");
                if (successID.equals("1")) {
                Toast.makeText(getApplicationContext(),successID,Toast.LENGTH_SHORT).show();
            } else if (successID.equals("0")) {
                Toast.makeText(getApplicationContext(),successID,Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    validate vld = new validate();
    vld.execute();
}

这里是 userFunction Activity:

 private static String userValidationURL = "http://58.26.106.181/b160088b/android/userSignSignIn.php";

public JSONObject userValidation(String email , String strPassword) {
    // Building Parameters
    String data = "";
    try {
        Log.e("email",email);
        data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8");
        data = URLEncoder.encode("Password", "UTF-8") + "=" + URLEncoder.encode(strPassword, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    JSONObject json = jsonParser.getJSONFromUrl(userValidationURL, data);
    return json;
}

这是 PHP 文件:

<?php
include ("config.php");
//$email = "nomoneynotalk94@gmail.com";
$email = $_POST['email'];
$pass = $_POST['Password'];
$sql_query = "SELECT * FROM user where email ='".$email."' AND password ='".$pass."' ";
$result = mysqli_query($conn,$sql_query);
$num_rows = mysqli_num_rows($result);
if($num_rows > 0 ) {
    // return login sucessful
    $row = mysqli_fetch_array($result);
    $response["success"] = 1;
} else {
    //return login failed
    $response["success"] = 0;
}
echo json_encode($response);
?>

这是错误:

06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at zr.suc.june19.BackEnd.JSONParser.getJSONFromUrl(JSONParser.java:59)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at zr.suc.june19.BackEnd.UserFunctions.userValidation(UserFunctions.java:53)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at zr.suc.june19.BeforeLogin.SignIn$1validate.doInBackground(SignIn.java:89)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at zr.suc.june19.BeforeLogin.SignIn$1validate.doInBackground(SignIn.java:75)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-21 15:07:14.246 16569-16953/zr.suc.june19 W/System.err:     at java.lang.Thread.run(Thread.java:818)

【问题讨论】:

  • 你没有得到 json。看起来你得到一个错误页面,它是 HTML
  • 顺便说一句:了解准备好的语句以防止 SQL 注入。另外从不将密码存储为纯文本。并始终通过https 或以加密形式传输。
  • 在任何在线 json 解析器中检查您的输出。

标签: java php android json


【解决方案1】:

您的内容字符串包含 JSON 字符串(用大括号 {} 括起来的字符串),但它还包含需要删除的 HTML 部分。

在您尝试创建 JSONObject 之前输入此代码:

int jsonStart = Content.indexOf("{");
int jsonEnd = Content.lastIndexOf("}");

if (jsonStart >= 0 && jsonEnd >= 0 && jsonEnd > jsonStart) {
    Content = Content.substring(jsonStart, jsonEnd + 1);
} else {
    // deal with the absence of JSON content here
}

即看起来它正在返回 html 部分,因此需要省略 html 部分并且只需要获取 json 部分 希望有帮助!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2017-10-02
    • 2013-08-02
    • 1970-01-01
    相关资源
    最近更新 更多