Getting data from ubidots to android app

I am having problem to get data from ubidots to my android app, i am using android studio and here is my code: (problem: my android app is exiting itself), please help me.

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import com.ubidots.*;
import com.ubidots.ApiClient;
import com.ubidots.Variable;

class MyActivity extends Activity {
    private TextView ecgLevel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ecgLevel = (TextView) findViewById(R.id.ecg);

        public class ApiUbidots extends AsyncTask<Integer, Void, Value[]> {
            private final String API_KEY = " i entered API key here";
            private final String VARIABLE_ID = "i entered my variable id here";

            @Override
            protected Value[] doInBackground(Integer... params) {
                ApiClient apiClient = new ApiClient(API_KEY);
                Variable ecg = apiClient.getVariable(VARIABLE_ID);
                Value[] variableValues = ecg.getValues();

                return variableValues;
            }

            @Override
            protected void onPostExecute(Value[] variableValues) {
                Double variableValue = variableValues[0].getValue();
                ecgLevel.setText(String.valueOf(variableValue));
            }
        }
    }
}

Hello @jyoti_prakash_uprety ,
Thank you for reaching out to Ubidots’ Community Forum.

Could you please share, if any, the logs of what is going on with your app? This could help us troubleshoot what could be causing the unexpected exiting of your app).

On the other hand, I’d like to let you know that I understand the benefit and simplicity of using the our libraries, however, we won’t be updating our libraries soon. This is because we opted to focus on exposing as many API endpoints and entities as possible (devices, organizations, users, events, etc…) as opposed to growing in terms of SDK/API libraries support. Having said that, I think that the reason for your code’s errors could be due to the fact that our SDK libraries are quite old and might have unsupported code for today’s libraries.

I’ll be attentive to your response and to your Logs, should you have them.

Best regards,
Sebastián

1 Like

Thank you for your response, well i manage to get value form ubidots to my andorid app but now my problem is: i want to get real time value which will be store in array and when ever the value is changed it should be updated in array. Here is my code please help me to modify it.

public class MainActivity extends Activity {
private TextView ecgLevel;
String variableValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ecgLevel = (TextView) findViewById(R.id.ecg);
    ApiUbidots getApi = new ApiUbidots();
    getApi.execute();
}


class ApiUbidots extends AsyncTask<Integer, Void, Value[]> {
    private final String API_KEY = "************";
    private final String VARIABLE_ID = "************";

    @Override
    protected Value[] doInBackground(Integer... params) {
        ApiClient apiClient = new ApiClient(API_KEY);
        Variable ecgLevel = apiClient.getVariable(VARIABLE_ID);
        Value[] variableValues = ecgLevel.getValues();

        return variableValues;
    }

    @Override
    protected void onPostExecute(Value[] variableValues) {
        double varValue = variableValues[0].getValue();
        variableValue = (String.valueOf(varValue));
        ecgLevel.setText(variableValue);
    }
}

}

Hello @jyoti_prakash_uprety

The library that I think you are using (this one), only supports HTTP requests, which means that you would have to poll the data in order to get somewhat close to real-time results. For example, use the getvalue() method every 5 seconds. This will give you the value for that variable and when it changes, the longest it would take your code to notice the change would be 5 seconds. One thing to notice about this is that this heavily increases your Dots Out count, which depending on your account, could be somewhat small or large.

Regarding the code, I think that the getApi.execute() function could go inside a while function with a sleep method (this sleep is what makes the time between API calls), so that it continuously retrieves data within intervals.

Does this make sense?

Best regards