dongtangdao7232 2017-06-21 07:32
浏览 202

Android编程:java.lang.String类型的值无法转换为JSONObject

I'm trying to do a login page from an Android activity connecting the database. I'm getting this error "org.json.JSONException: Value

I am new to Android programming and I tried for three days. Thank you.

Here is activity code

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

Here is the 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;
}

Here is the PHP file:

<?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);
?>

Here is the error:

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)
  • 写回答

1条回答 默认 最新

  • dpmkif3097 2017-06-21 07:41
    关注

    Your Content string contains the JSON string (the one surrounded by braces {}) but it also contains an HTML part, which needs to be removed.

    Put this code before you try to create the 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
    }
    

    i.e. it looks it is returning html part and hence html part need to be omitted and need to take only the json part Hope that helps!!!

    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误