Volley的onResponse()未调用(代码运行正确,但有时不起作用)

时间:2019-09-02 05:56:39

标签: java android android-volley

我已经在家中正确测试了以下代码,没有错误。
但这相同的代码在办公室不起作用(尤其是在排球,数据库连接部分周围)

主要问题是“未调用onResponse()”

-这种情况是什么问题?
-如何查看与此问题相关的日志或信息?

// ============================================== ===============
Java代码文件

package com.example.mpandroidcharttest;

import androidx.appcompat.app.AppCompatActivity;

...

// ============================================================
public class BarChartPosNegActivity extends AppCompatActivity
{
    // ============================================================
    BarChart barChart;
    ImageView iv_normal, iv_abnormal;

    public static String user_ID_passed_one = "";

    // ============================================================
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        // ============================================================
        super.onCreate(savedInstanceState);

        // ============================================================
        setContentView(R.layout.activity_bar_chart_pos_neg);

        // ============================================================
        barChart = findViewById(R.id.mp_BarChart);
        ...

        // ============================================================
        String passed_user_ID = "first_ID";
        BarChartPosNegActivity.user_ID_passed_one = passed_user_ID;

        // ============================================================
        get_refDB_up_down_numerical_table();
    }

    // ============================================================
    private ArrayList<BarEntry> dataValues1()
    {
        ArrayList<BarEntry> dataVals = new ArrayList<>();
        dataVals.add(new BarEntry(0f, 1000));
        dataVals.add(new BarEntry(1f, 25));
        return dataVals;
    }

    // ============================================================
    void chart_algorithm(JSONArray jsonArray_data)
    {
        // c local_urine_protein_criterion: urine protein criterion in local scope
        HashMap<String, String> local_urine_protein_criterion = new HashMap<>();

        // ============================================================
        try {
            // c ref_data: get ref data source (0th record)
            JSONObject ref_data_source = jsonArray_data.getJSONObject(0);

            // ============================================================
            // c hashmap_ref_data: ref data (JSONObject -> hashmap)
            HashMap<String, String> hashmap_ref_data = new HashMap<>();

            JSONArray keys = ref_data_source.names();
            for (int i = 0; i < keys.length(); ++i) 
            {
                String key = keys.getString(i); // Here's your key
                String value = ref_data_source.getString(key); // Here's your value

                if (key.substring(0, 5).equals("ref__")) 
                {
                    hashmap_ref_data.put(key, value);
                }
            }
            ...
        } 
        catch (JSONException e) 
        {
            System.out.println(e);
        }

        // ============================================================
        BarDataSet barDataSet1 = new BarDataSet(dataValues1(), "DataSet 1");

        // ...

        // ============================================================
        // Pass barData to barChart
        barChart.setData(barData);

        barChart.invalidate();

        // ============================================================
        // ============================================================
        int urine_protein = 1;

        if (urine_protein == 1) 
        {
            iv_normal.setImageResource(R.drawable.posneg_neg_gray);
            iv_abnormal.setImageResource(R.drawable.posneg_pos_red);
        } 
        else if (urine_protein == 0) 
        {
            iv_normal.setImageResource(R.drawable.posneg_neg_green);
            iv_abnormal.setImageResource(R.drawable.posneg_pos_gray);
        }
    }

    // ============================================================
    void get_refDB_up_down_numerical_table()
    {
        Response.Listener<String> responseListener = new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response)
            {
                System.out.println("response response response response response response response response response response response ");
                System.out.println(response);

                try 
                {
                    JSONArray jsonArray = new JSONArray(response);
                    System.out.println(jsonArray);
                    chart_algorithm(jsonArray);

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

        RefDBNumericRequest loginRequest = new RefDBNumericRequest(responseListener);
        RequestQueue queue = Volley.newRequestQueue(BarChartPosNegActivity.this);
        queue.add(loginRequest);
    }
}

// ============================================== ===============
请求Java代码文件

package com.example.mpandroidcharttest;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;

import java.util.HashMap;
import java.util.Map;

class RefDBNumericRequest extends StringRequest
{
    final static private String med_data_TBL = "http://example.com/med_data_and_ref_data_joined_TBL.php";
    private Map<String, String> map;

    // ============================================================
    // Constructor
    public RefDBNumericRequest(Response.Listener<String> listener)
    {
        // ============================================================
        super(Method.POST, med_data_TBL, listener, null);

        // ============================================================
        map = new HashMap<>();

        String userID = "first_ID";
        map.put("userID", userID);
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError
    {
        return map;
    }
}

// ============================================== ===============
PHP文件

<?php
    $con = mysqli_connect("localhost", "my_id", "db_pass", "db_name");

    mysqli_query($con,'SET NAMES utf8');

    $userID = $_POST["userID"];

    // ============================================================
    $query = "SELECT * from med_data_TBL left join ref_db_TBL on med_data_TBL.user__recordID=ref_db_TBL.ref__DB_recordID";

    $result = mysqli_query($con, $query);

    $json = mysqli_fetch_all($result, MYSQLI_ASSOC);
    echo json_encode($json);

    mysqli_close($con);

?>

// ============================================== ===============
在onResponse()中返回的值

[{"user__recordID":"1","user__userID":"first_ID","user__urine_protein__posneg":"positive","user__glucose__down_up_numerical":"80","user__systolic_blood_pressure__down_up_numerical":"140","ref__DB_recordID":"1","ref__urine_protein__posneg":"negative","ref__glucose__down_up_numerical_down":"60","ref__glucose__down_up_numerical_up":"120","ref__systolic_blood_pressure__down_up_numerical_down":"60","ref__systolic_blood_pressure__down_up_numerical_up":"140"},
 {"user__recordID":"2","user__userID":"second_ID","user__urine_protein__posneg":"negative","user__glucose__down_up_numerical":"130","user__systolic_blood_pressure__down_up_numerical":"50","ref__DB_recordID":null,"ref__urine_protein__posneg":null,"ref__glucose__down_up_numerical_down":null,"ref__glucose__down_up_numerical_up":null,"ref__systolic_blood_pressure__down_up_numerical_down":null,"ref__systolic_blood_pressure__down_up_numerical_up":null},
 {"user__recordID":"3","user__userID":"third_ID","user__urine_protein__posneg":"positive","user__glucose__down_up_numerical":"50","user__systolic_blood_pressure__down_up_numerical":"120","ref__DB_recordID":null,"ref__urine_protein__posneg":null,"ref__glucose__down_up_numerical_down":null,"ref__glucose__down_up_numerical_up":null,"ref__systolic_blood_pressure__down_up_numerical_down":null,"ref__systolic_blood_pressure__down_up_numerical_up":null}]

// ============================================== ===============
编辑
在办公室,我再次遇到以下消息(相同的程序包,但签名不同,当我在两个不同的地方(例如家庭和办公室)运行相同的代码时)。
enter image description here

然后我单击“确定”,现在可以调用截击的onResponse()。
如果有人知道为什么出现此消息,以及如果该问题实际上与该消息有关,如何解决此问题,请指导我。
/mnt/external_disk/Capture_temp/2019_09_03_09:04:16.png

1 个答案:

答案 0 :(得分:-1)

回到家后,我再次运行了相同的代码(该代码不在办公室运行)

运行正常。我看到以下消息是此问题的线索:

bitcoin/bitcoin.hpp