通过Android.Volley的POST方法不会更新数据库,VolleyError只会返回“ 8.onErrorResponse:”

时间:2019-03-25 19:38:03

标签: android mysql json post android-volley

我正在尝试使用Volley从我的应用程序更新MySQL数据库。我用来检索数据的POST方法可以正常工作,但是我无法终生更新数据库。 VolleyError只是在Logcat中给出8.onErrorResponse:error。

我已经运行了shell命令以在logcat中获取详细消息,并且似乎可以发送请求。我从Logcat得到的结果是(我将网址匿名了):

2019-03-25 17:52:45.122 9882-9917/wlv.ac.uk.homeautomation D/Volley: [1292] WaitingRequestManager.maybeAddToWaitingRequests: new request, sending to network 1-http://mywebaddress/updateTemp.php 2019-03-25 17:52:45.616 9882-9918/wlv.ac.uk.homeautomation D/Volley: [1293] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://mywebaddress/updateTemp.php 0x20cac14a NORMAL 1> [lifetime=494], [size=191], [rc=200], [retryCount=0] 2019-03-25 17:52:45.618 9882-9882/wlv.ac.uk.homeautomation V/Volley: [1] 8.onErrorResponse: ERROR 2019-03-25 17:52:45.618 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (497 ms) [ ] http://mywebaddress/updateTemp.php 0x20cac14a NORMAL 1 2019-03-25 17:52:45.619 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+0 ) [ 1] add-to-queue 2019-03-25 17:52:45.619 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+0 ) [1292] cache-queue-take 2019-03-25 17:52:45.619 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+0 ) [1292] cache-miss 2019-03-25 17:52:45.620 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+1 ) [1293] network-queue-take 2019-03-25 17:52:45.621 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+495 ) [1293] network-http-complete 2019-03-25 17:52:45.621 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+0 ) [1293] network-parse-complete 2019-03-25 17:52:45.622 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+0 ) [1293] post-response 2019-03-25 17:52:45.622 9882-9882/wlv.ac.uk.homeautomation D/Volley: [1] MarkerLog.finish: (+1 ) [ 1] done

我的Java代码是:

private void setTemperature (int desiredTemp, int roomId) {
        tempToAchieve.setText(getString(R.string.temp_to_achieve, 
temperature));
        newTemp = temperature;
        String url = "http://mywebaddress/updateTemp.php";
        VolleyLog.DEBUG = true;
        RequestQueue queue = Volley.newRequestQueue(getActivity());
        VolleyLog.setTag("Volley");
        Log.isLoggable("Volley", Log.VERBOSE);
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("roomID", roomId);
            jsonObject.put("desiredTemp", desiredTemp);
            JsonObjectRequest json = new 
JsonObjectRequest(Request.Method.POST, url, jsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            VolleyLog.d(TAG, response.toString());
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error){
                    VolleyLog.v(TAG, error.getMessage());
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }
            };
            queue.add(json);
        } catch (JSONException e) {
            Log.d(TAG, e.getMessage());
            }
    }
}

您可能会看到我拼命尝试让VolleyLog打印出有用的东西...!

我的PHP脚本在下面(我是所有这些的新手...):

<?php


$servername = "server";
$username = "uname";
$password = "password";
$dbname = "db";


$conn = new mysqli_connect($servername, $username, $password, $dbname);

$desiredTemp=$_POST['desiredTemp'];
$roomID=$_POST['roomID'];

$sql = "UPDATE room SET desiredTemp = '$desiredTemp' WHERE roomID = 
'$roomID'";


if (mysqli_query($conn, $sql) {
    echo "success"
} else {
    echo "error"
}



mysqli_close($conn);

?>

提前谢谢...我不知道!

干杯。

0 个答案:

没有答案