Android Wear Volley onResponse 错误 null

Android Wear Volley onResponse error null

我正在尝试使用我的 Sony Smartwatch 3 从 AWS 获取数据。但是,在使用 Volley 处理数据时,我总是得到 "Error: null"。它可以在我的 android 智能手机上使用确切的一些代码运行,但在智能手表上遇到错误。

这是我的 activity 代码

button2 = (Button) findViewById(R.id.button2);

        button2.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View v) {
                getTemperature();
                //displaySpeechRecognizer();
            }
        });

还有我的截击代码

 private void getTemperature() {
        // Tag used to cancel the request
        String tag_string_req = "req_login";

        pDialog.setMessage("Getting data ...");
        showDialog();

        StringRequest strReq = new StringRequest(Request.Method.GET,
                AppConfig.URL_TEMPERATURE, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "AWS-Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);

                    JSONObject user = jObj.getJSONObject("user");
                    String temperature = user.getString("value");
                    String humidity = user.getString("value2");

                    String totals = "The temperature is "+temperature+" and humidity is "+humidity;
                    total.setText(totals);

                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "AWS Error: " + error.getMessage());
                hideDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

这是预期的 JSON 响应

{"user":{"value":"23.0","value2":"41.0"}}

和当前错误

04-04 15:32:49.229 27078-27078/com.sp.weargetfromaws E/MainActivity: AWS Error: null

和 php 文件

<?php


// json response array
$response = array();

$config = parse_ini_file('./private/config.ini'); 

$link = mysqli_connect($config['servername'],$config['username'],$config['password'],$config['dbname']);
$tablename = $config['tablenamevalue'];

$sql = "SELECT * FROM ".$tablename." WHERE serialno = 'a' ORDER BY ID DESC LIMIT 1  ";

    if($result = mysqli_query($link, $sql))
    {
        if(mysqli_num_rows($result) > 0)
        {

            while($row = $result->fetch_array(MYSQLI_BOTH))
            {

                $response["user"]["value"] = $row["value"];
                $response["user"]["value2"] = $row["value2"];
            }

            echo json_encode($response);

        }

    }



?>

如果您不需要使用 GET 方法将任何值推送到您的 API 代码,请尝试 JsonArrayRequest

您的 php 文件:

$config = parse_ini_file('./private/config.ini'); 

$link = mysqli_connect($config['servername'],$config['username'],$config['password'],$config['dbname']);
$tablename = $config['tablenamevalue'];

$sql = "SELECT * FROM ".$tablename." WHERE serialno = 'a' ORDER BY ID DESC LIMIT 1  ";

 $result = mysqli_query($link,$sql); 
 $res = array(); 

 while($row = mysqli_fetch_array($result)){
 array_push($res, array(
 "value1"=>$row['value1'],
 "value2"=>$row['value2'],   
 ));
 }

 echo json_encode($res, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_ERROR_UTF8);

你的排球代码:

 private void getTemperature() {
        // Tag used to cancel the request
        String tag_string_req = "req_login";

        pDialog.setMessage("Getting data ...");
        showDialog();

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(AppConfig.URL_TEMPERATURE,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        parseData(response);
                        progressBar.setVisibility(View.GONE);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity().getApplicationContext(), "Thats all", Toast.LENGTH_SHORT).show();
                        //Hiding the progressbar
                    }
                });
    return jsonArrayRequest;
    }


        private void parseData(JSONArray array) {
        String temperature = null;
        String humidity = null;
        String totals = null;

        Log.d("DebugTag", "Value array.length() - parseData:" + array.length());
        for (int i = 0; i < array.length(); i++) {

            JSONObject json = null;
            try {

                //Getting json
                json = array.getJSONObject(i);

                temperature = json.getString("value1");
                humidity = json.getString("value2");
                totals = "The temperature is "+temperature+" and humidity is "+humidity;               

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        total.setText(totals);
}