Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php StringRequest响应为空,没有明显的原因_Php_Android_Json_Android Volley - Fatal编程技术网

Php StringRequest响应为空,没有明显的原因

Php StringRequest响应为空,没有明显的原因,php,android,json,android-volley,Php,Android,Json,Android Volley,对于我的原始PHP web服务public void onResponseString响应,响应包含以下响应: {成功:1,消息:Spot已成功添加!} 对于我更新的PHP web服务,响应是空的,不存在 在我的浏览器中运行PHP web服务时,我得到以下JSON对象: {success:1,message:Spot已成功添加!,如_id:428} 这是伟大的,但我希望在应用程序中的响应看起来一样 那么:原因是什么?我如何使JSON对象出现在响应中 Java代码 原始web服务PHP: 对于脚本

对于我的原始PHP web服务public void onResponseString响应,响应包含以下响应:

{成功:1,消息:Spot已成功添加!}

对于我更新的PHP web服务,响应是空的,不存在

在我的浏览器中运行PHP web服务时,我得到以下JSON对象:

{success:1,message:Spot已成功添加!,如_id:428}

这是伟大的,但我希望在应用程序中的响应看起来一样

那么:原因是什么?我如何使JSON对象出现在响应中

Java代码

原始web服务PHP:


对于脚本为什么在web浏览器中显示正确的JSON对象,而在截击响应中却什么都没有,我感到很惊讶

实现在新的web服务PHP中所做工作的更好方法是只执行一个查询,即原始web服务PHP中的查询,并使用


根据您用于MySql连接的方法。

在$getId查询中,尝试将WHERE子句更改为LATIONE=:lat和LATIONE=:LNGY您的新web服务PHP容易受到sql注入攻击。在SELECT中使用准备好的语句,就像在INSERT中一样。这与你的计划无关problem@ColinGillespie当我回显$getId时,您的解决方案将从纬度=:纬度和经度=:lng限制1的位置提供SELECT id,这将不起作用..因为您无法通过新的web服务获得答案,显然如果$row{失败。@user1736049抱歉:lng应该是:long
public void writeLikedPos(LatLng location) {
        double latitude;
        double longitude;
        try {
            latitude = location.latitude; //nullpointer
            longitude = location.longitude;
        } catch (NullPointerException e) {
            System.out.println("Noe gikk galt!");
            e.printStackTrace();
            return;
        }
        final String latitudeStr = String.valueOf(latitude);
        final String longitudeStr = String.valueOf(longitude);

        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println("String response: " +response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("Error");
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("latitude", latitudeStr);
                params.put("longitude", longitudeStr);

                return params;
            }
        };
        VolleySingleton.getInstance().addToReqQueue(postRequest);
    }
}
<?php
//load and connect to MySQL database stuff
require("config.inc.php");

if (!empty($_POST)) {


    //initial query
    if(isset($_POST['user_id']) && !empty($_POST['user_id'])) {
        $query = "INSERT INTO spot ( latitude, longitude, user_id ) VALUES ( :lat, :long, :uid ) ";
        //Update query
        $query_params = array(
            ':lat' => $_POST['latitude'],
            ':long' => $_POST['longitude'],
            ':uid' => $_POST['user_id']
            );

    } else {
        $query = "INSERT INTO spot ( latitude, longitude ) VALUES ( :lat, :long ) ";
        //Update query
        $query_params = array(
            ':lat' => $_POST['latitude'],
            ':long' => $_POST['longitude']
            );
    }
    //execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't add lat/long-pair!";
        die(json_encode($response));
    }


    $latt = $_POST['latitude'];
    $longg = $_POST['longitude'];

    $getId = "SELECT id FROM spot WHERE latitude=$latt AND longitude=$longg LIMIT 1";
    //execute query
    try {
        $stmt2   = $db->prepare($getId);
        $result2 = $stmt2->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't retrieve like_id!";
        die(json_encode($response));
    }

    $row = $stmt2->fetchAll();
    if($row) {

    $response["success"] = 1;
    $response["message"] = "Spot Successfully Added!";
    $response["like_id"] = $row[0]['id'];
    echo json_encode($response);
    }

} else {
    ?>
    <h1>Registrer GPS-koordinater</h1>
    <form action="idtest.php" method="post">
        Latitude:<br />
        <input type="text" name="latitude" value="" />
        <br /><br />
        Longitude:<br />
        <input type="text" name="longitude" value="" />
        <br /><br />
        Google ID:<br />
        <input type="text" name="user_id" value="" />
        <br /><br />
        <input type="submit" value="Opprett kolonne i 'spot'" />
    </form>
    <?php
}

?>
<?php
// TODO: Lag en sjekk for at lat/long faktisk er lat/long-verdier.

//load and connect to MySQL database stuff
require("config.inc.php");

if (!empty($_POST)) {
    //initial query
    if(isset($_POST['user_id']) && !empty($_POST['user_id'])) {
        $query = "INSERT INTO spot ( latitude, longitude, user_id ) VALUES ( :lat, :long, :uid ) ";
        //Update query
        $query_params = array(
            ':lat' => $_POST['latitude'],
            ':long' => $_POST['longitude'],
            ':uid' => $_POST['user_id']
            );

    } else {
        $query = "INSERT INTO spot ( latitude, longitude ) VALUES ( :lat, :long ) ";
        //Update query
        $query_params = array(
            ':lat' => $_POST['latitude'],
            ':long' => $_POST['longitude']
            );
    }
    //execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Database Error. Couldn't add lat/long-pair!";
        die(json_encode($response));
    }

    $response["success"] = 1;
    $response["message"] = "Spot Successfully Added!";
    echo json_encode($response);

} else {
    ?>
    <h1>Registrer GPS-koordinater</h1>
    <form action="addspottest.php" method="post">
        Latitude:<br />
        <input type="text" name="latitude" value="" />
        <br /><br />
        Longitude:<br />
        <input type="text" name="longitude" value="" />
        <br /><br />
        Google ID:<br />
        <input type="text" name="user_id" value="" />
        <br /><br />
        <input type="submit" value="Opprett kolonne i 'spot'" />
    </form>
    <?php
}

?>