[SOLVED] How to get latest value on Android app

I want to get the latest value on my android app in real time. i’m not using ubidots library, i’m sending a json request (as described in API documentation) and i’m successfully getting values in my Charts but i don’t know how to get the lat latest value and show it in text view. here is my code
Class where i’m handing Json response

  public class UbidotsClient {
  private UbiListener listener;

public UbiListener getListener() {
    return listener;
}

public void setListener(UbiListener listener) {
    this.listener = listener;
}

public void handleUbidots(String Datasource ,String varId, String apiKey, final UbiListener listener) {

    final List<Value> results = new ArrayList<>();

    OkHttpClient client = new OkHttpClient();
    Request req = new Request.Builder().addHeader("X-Auth-Token", apiKey)
            .url("http://things.ubidots.com/api/v1.6/devices/" + Datasource + "/" + varId + "/values")
            .build();

    client.newCall(req).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            Log.d("Chart", "Network error");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Response response) throws IOException {
            String body = response.body().string();
            Log.d("Chart", body);

            try {
                JSONObject jObj = new JSONObject(body);
                JSONArray jRes = jObj.getJSONArray("results");
                for (int i=0; i < jRes.length(); i++) {
                    JSONObject obj = jRes.getJSONObject(i);
                    Value val = new Value();
                    val.timestamp = obj.getLong("timestamp");
                    val.value  = (float) obj.getDouble("value");
                    results.add(val);
                }

                listener.onDataReady(results);

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

        }
    });

}


public static class Value {
    public float value;
   public long timestamp;

}

public interface  UbiListener {
    public void onDataReady( List<Value> result);
}

}
Fragment where i want to show the values

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_monitor, container, false);
    heart_rate = (TextView) view.findViewById(R.id.heart);
  
    
    callAsynchronousTask();
    
    // Inflate the layout for this fragment
    return view;
}

public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    try{
                        (new UbidotsClient()).handleUbidots(Datasource,heartVarId, API_KEY, new UbidotsClient.UbiListener() {
                            @Override
                            public void onDataReady(List<UbidotsClient.Value> result) {
                                List <> values =
                            }
                        });
                    }
                    catch (Exception e){
                        android.util.Log.i("Error", "Error");
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 2000);
}

As i described above i’m successfully getting values in my an other fragment where i’m using chart to display all values

Greetings,

According to the API you should use the ‘page_size’ keyword to manage the amount of data to retrieve, so if you want to get your last value simple add ‘page_size=1’ to your endpoint:

http://things.ubidots.com/api/v1.6/variables/{VAR_ID}/values/?token={TOKEN}&page_size=1

Regards

Hi zJ99,

Can share the final running code? or share git link to check with
This code through some error i have the same task to execute.