检查新值是否与旧值相同

时间:2016-04-27 07:11:07

标签: android

我从网络服务获取的价值很少。每次从Web服务获取值时,值都会不断变化。我在TimerTask中使用Service来重复获取值。

我无法想到如何检查我得到的价值是否与以前相同。

      public ArrayList getCo_ordinates(String deviceId) {
        String URL_CO_ORDINATES = "http://192.168.1.42:8080/image/getDevicePosition?deviceId=" + deviceId;

//            trimCache(getApplicationContext());
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.start();

        final JsonArrayRequest request = new JsonArrayRequest(URL_CO_ORDINATES, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                try {

                    for (int i = 0; i < response.length(); i++) {

                        X = response.getJSONObject(i).getString("xCoordinate");
                        Y = response.getJSONObject(i).getString("yCoodinate");
                            System.out.println("xCoordinate" + X);
                            System.out.println("yCoodinate" + Y);
//                                tap();
                        addTap(Integer.parseInt(X), Integer.parseInt(Y));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(context,""+e,Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "Could Not connect to server", Toast.LENGTH_LONG).show();

            }
        });
        requestQueue.add(request);


        return null;
    }

1 个答案:

答案 0 :(得分:0)

     X = response.getJSONObject(i).getString("xCoordinate");
     Y = response.getJSONObject(i).getString("yCoodinate");

     System.out.println("xCoordinate" + X);
     System.out.println("yCoodinate" + Y);

  if(prev_x==X)
  {
  Log.d("same","value");
  }
  else
  {
  Log.d("different","value");
  }

  prev_x=X;
  prev_y=Y;