duandongji2231 2019-04-04 17:38
浏览 66

Volley Android org.json.JSONException出错:java.lang.String类型的值<br无法转换为JSONObject

I'm trying to do a login to my app with a XAMMP server. I'm doing the conexion with the server with Volley, but I don't understand why y have this error. I have looked for many questions in stackoverflow and I can't find out the solution. This is the android code:

public class Login extends AppCompatActivity {

    RequestQueue queue;
    JsonObjectRequest request;
    Map<String, String> map = new HashMap<String, String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(activity_login);
        getSupportActionBar().hide();//<!-- Para que no salga la barra superior en todas las activitis-->
    }
    public void sayHelloThreads(final View view) throws IOException { //log in


        EditText USUARIO =
                (EditText) findViewById(R.id.Usuario);


        String usu = USUARIO.getText().toString();
        EditText Password =
                (EditText) findViewById(R.id.Pass);


        String pass = Password.getText().toString();
        // the request queue
        queue = Volley.newRequestQueue(this);

        // the parameters for the php
        // map.put(KEY, VALUE);
        map.put("uname", usu);
        map.put("psw", pass);

        // the JSON request
        // JsonObjectRequest(METHOD, URL, JSONOBJECT(PARAMETERS), OK_LISTENER, ERROR_LISTENER);

        request = new JsonObjectRequest(
                Request.Method.POST, // the request method
                "http://192.168.1.41:8080/check-login_APP_voluntarios.php", // the URL
                new JSONObject(map), // the parameters for the php
                new Response.Listener<JSONObject>() { // the response listener
                    @Override
                    public void onResponse(JSONObject response){
                        // here you parse the json response
                        String pp="lsjhjh";
                    }
                },
                new Response.ErrorListener() { // the error listener
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        String pp="lsjhjh";
                      /* here you can warn the user that there
                      was an error while trying to get the json
                      information from the php  */
                    }
                });

        // executing the quere to get the json information
        queue.add(request);

    }
}

And this the code of the php file:

<?php

$con = mysqli_connect('localhost','root','','rot_v2');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");

//$usuario = $_POST['uname']; //uname
//$password = $_POST['psw'];
// decoding the json array
 $post = json_decode(file_get_contents("php://input"), true);
 // getting the information from the array
    // in the android example I've defined only one KEY. You can add more KEYS to your app

   $usuario = $post['uname'];
   $password = $post['psw'];
   // the "params1" is from the map.put("param1", "example"); in the android code
   // if you make a "echo $my_value;" it will return a STRING value "example"



$sql="SELECT `voluntarios`.`Password` FROM `voluntarios` WHERE (`voluntarios`.`Usuario` like'%{$usuario}%')";
$result = mysqli_query($con,$sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {

        $passwordres = $row['Password'];
    }

    //echo "<script>console.log( 'Debug Objects: " . $passwordres . "' );</script>";


    if ($password == $passwordres){

        //echo "Correcta voluntario";
        $response = "OK"
        header('Content-Type: application/json');
        echo(json_encode($response));

    }
    else{
        $response = "KO"
        header('Content-Type: application/json');
        echo(json_encode($response);
    }

}
else{
    $response = "KO2"
    header('Content-Type: application/json');
    echo(json_encode($response);

}



mysqli_close($con);
/*
if($tipousuario == "corredor"){

echo "Se ha enviado a un corredor";

}if($tipousuario == "voluntario"){
    echo "Se ha enviado a un corredor";
}*/

?>

This is a screenshoot of the error: enter image description here

Can you help me with this trouble, thanks you!!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行