返回片段

时间:2018-06-28 07:26:25

标签: java android android-volley

我正在执行一项任务,如果之后在onCreate中命中了URL,我将把所有URL存储在Click中的对象的哈希映射库中。我正在访问特定的对象及其工作正常。但是在状态后如果按返回按钮然后再次返回片段状态的URL统计信息仅是什么建议呢? 我的JSON响应:

[
    {
        "id": 1404,
        "cameraId": 1184,
        "onUrl": "",
        "offUrl": "",
        "statusUrl": "",
        "type": "Strobe",
        "statusOnMessage": "P1 = on",
        "statusOffMessage": "P1 = off",
        "onSuccessMessage": "P1 = on",
        "offSuccessMessage": "P1 = off"
    },
    {
        "id": 1405,
        "cameraId": 1184,
        "onUrl": "",
        "offUrl": "",
        "statusUrl": "",
        "type": "Light",
        "statusOnMessage": "P2 = on",
        "statusOffMessage": "P2 = off",
        "onSuccessMessage": "P2 = on",
        "offSuccessMessage": "P2 = off"
    }
]

**访问此URL后,我将所有数据存储在哈希图中,并在单击事件中访问该URL,但再次返回相同的片段后,此状态仅在预览状态下返回** 代码:

private void getActionURl() {
    final String potentialId = SitesUtil.getInstance().getSelectedPotentialId();

    String urlOne = UrlUtility.getActionUrl(potentialId, cameraId);
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    final StringRequest stringRequest = new StringRequest(Request.Method.GET, urlOne, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Gson gson = new Gson();
            try {
                ActionButton[] actionButton = gson.fromJson(response, ActionButton[].class);
                for (ActionButton actionButton1 : actionButton) {
                    actionButtonMap.put(actionButton1.getType(), actionButton1);


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


        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(stringRequest);

}@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.imageButton:


        /*  final  ActionButton*/
            btnConfig = actionButtonMap.get("Strobe");
            if (btnConfig == null) {

                Toast.makeText(getContext(), "Sensor Not Available", Toast.LENGTH_SHORT).show();
                return;
            }

        {
            if (isStrobe) ----> its local Boolean value{
                RequestQueue requestQueue = Volley.newRequestQueue(getContext());
                StringRequest stringRequest = new StringRequest(Request.Method.GET, btnConfig.getOnUrl(), new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        try {
                            Toast.makeText(getContext(), btnConfig.getOnSuccessMessage(), Toast.LENGTH_SHORT).show();

                            isStrobe = false;

                            imageButton.setImageResource(R.drawable.ic_alarm);
                        } catch (Exception e) {
                            e.printStackTrace();

                        }


                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                requestQueue.add(stringRequest);
            } else {
                RequestQueue requestQueue = Volley.newRequestQueue(getContext());
                StringRequest stringRequest = new StringRequest(Request.Method.GET, btnConfig.getOffUrl(), new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Toast.makeText(getContext(), btnConfig.getOffSuccessMessage(), Toast.LENGTH_SHORT).show();
                        imageButton.setImageResource(R.drawable.ic_alarm_second);
                        isStrobe = true;
                        Utils.saveToSharedPreferenceBoolean("isStrobe", isStrobe);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                requestQueue.add(stringRequest);
            }

        }
        break;
        case R.id.imageButton2:
            final ActionButton btnConfigOne = actionButtonMap.get("Light");
            if (btnConfigOne == null) {
                /*imageButtonOne.setVisibility(View.GONE);*/
                Toast.makeText(getContext(), "Sensor Not Available", Toast.LENGTH_SHORT).show();
                return;
            }


            if (isLight) {

                RequestQueue requestQueue = Volley.newRequestQueue(getContext());
                StringRequest stringRequest = new StringRequest(Request.Method.GET, btnConfigOne.getOnUrl(), new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Toast.makeText(getContext(), btnConfigOne.getOnSuccessMessage(), Toast.LENGTH_SHORT).show();

                        isLight = false;

                        imageButtonOne.setImageResource(R.drawable.ic_light_bulb);
                        Utils.saveToSharedPreferenceBoolean("isLight", isLight);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(stringRequest);
            } else {
                RequestQueue requestQueue = Volley.newRequestQueue(getContext());
                StringRequest stringRequest = new StringRequest(Request.Method.GET, btnConfigOne.getOffUrl(), new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getContext(), btnConfigOne.getOffSuccessMessage(), Toast.LENGTH_SHORT).show();

                        isLight = true;
                        imageButtonOne.setImageResource(R.drawable.ic_bulb_second);
                        //Utils.saveToSharedPreferenceBoolean("isLight", isLight);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(stringRequest);
            }

            break;
        case R.id.imageButton3:
            final ActionButton btnConfigMotion = actionButtonMap.get("Motion");
            if (btnConfigMotion == null) {
                /*imageButtonTwo.setVisibility(View.GONE);*/
                Toast.makeText(getContext(), "Sensor Not Available", Toast.LENGTH_SHORT).show();
                return;
            }

            if (isMotion) {
                RequestQueue requestQueue = Volley.newRequestQueue(getContext());
                StringRequest stringRequest = new StringRequest(Request.Method.GET, btnConfigMotion.getOnUrl(), new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getContext(), btnConfigMotion.getOnSuccessMessage(), Toast.LENGTH_SHORT).show();
                        isMotion = false;
                        imageButtonTwo.setImageResource(R.drawable.ic_motion_sensor);


                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(stringRequest);
            } else {
                RequestQueue requestQueue = Volley.newRequestQueue(getContext());
                StringRequest stringRequest = new StringRequest(Request.Method.GET, btnConfigMotion.getOffUrl(), new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getActivity(), btnConfigMotion.getOffSuccessMessage(), Toast.LENGTH_SHORT).show();
                        isMotion = true;
                        imageButtonTwo.setImageResource(R.drawable.ic_motion_sensor_seocond);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                });
                requestQueue.add(stringRequest);
            }
            break;
    }
}

0 个答案:

没有答案