简体   繁体   English

Android Wear Volley onResponse错误为空

[英]Android Wear Volley onResponse error null

I am trying to get data from AWS using my Sony Smartwatch 3. However, I always get "Error: null" when processing the data using Volley. 我正在尝试使用Sony Smartwatch 3从AWS上获取数据。但是,在使用Volley处理数据时,我总是会收到“错误:空”。 It works on my android smartphone using the exact some code but encounter error on the smart watch. 它使用确切的一些代码在我的android智能手机上工作,但在智能手表上遇到错误。

Here is my activity code 这是我的活动代码

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

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

and my volley code 和我的齐射代码

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

and this is the expected JSON response 这是预期的JSON响应

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

and the current error 和当前的错误

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

and the php file 和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);

        }

    }



?>

Try JsonArrayRequest if you dont need to push any values to your API code using GET method 如果您不需要使用GET方法将任何值推送到您的API代码,请尝试JsonArrayRequest

your php file: 您的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);

your volley code: 您的齐射代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM