Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

java - Live Json parse with Volley and postdelay

im not sure about my Code. And i hope u can understand what i try to do. And by the way i learning this, and im not a Profi. Have mercy upon me ??

I have a url that Response a Json with only this string

If Button On was clicked

{Power:On}

And if Button Off clicked

{Power:Off}

So i use this Code for parse the Json File to set the Button collors.

public class JSONStatusParser {



    String status;
    RequestQueue requestQueue;




    public JSONStatusParser() {
    }


       public void statusrequest(String deviceIp, Context view, Button on, Button off, ImageView connectionView){

        requestQueue = Volley.newRequestQueue(view);



        JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, "http://" + deviceIp + "/cm?cmnd=Power",null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    status = response.toString();
                    if (status.contains("OFF")) {
                        on.setBackgroundTintList(ColorStateList.valueOf(Color.WHITE));
                        off.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
                    } else if (status.contains("ON")) {
                        on.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
                        off.setBackgroundTintList(ColorStateList.valueOf(Color.WHITE));
                    } else {
                        on.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
                        off.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
                    }

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

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                       error.printStackTrace();
                       connectionView.setImageResource(R.drawable.ic_disconnected);

                    }
                });
     requestQueue.add(objectRequest);
}
}

In my onBindViewHolder i have a Cardview as Item with this two Buttons and i call the JsonParser like this to Update the Status.

autoUpdateHandler = new Handler();

autoUpdateHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        JSONStatusParser statusParser = new JSONStatusParser();
        statusParser.statusrequest(currentDevice.getDeviceIp(), holder.itemView.getContext(), holder.onButton, holder.offButton,holder.connectView);
       autoUpdateHandler.postDelayed(this, 1500);



    }
}, 1500);

It works, but im not sure about this. Is this a correct way to check or listen if a Json String Changes?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...