How to display variable value from ubidots.com to Android textview?


#1

Hi,

I want to display the speed (example 89.0) value in my android textview i tried this way below but i don’t getting any output please help me to get my first value.

public class MainActivity extends AppCompatActivity
{

private final String API_KEY = "";
private final String VARIABLE_ID = "";

private TextView mSpeed;

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

}
public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        GetApiUbidots GetApiUbidots = new GetApiUbidots ();
                        GetApiUbidots.execute();


                    } catch (Exception e) {
                        android.util.Log.i("Error", "Error");
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 2000);
}

public class ApiUbidots extends AsyncTask<String, Void, Void> {

    private final String API_KEY = "";
    private final String VARIABLE_ID = "";
    @Override
    protected Void doInBackground(Integer... params) {
        ApiClient apiClient = new ApiClient(API_KEY);
         mspeed = apiClient.getVariable(VARIABLE_ID);

        return String.valueOf(mspeed);
    }


protected void onPostExecute(String variableValues) {
    // Update your views here

    mSpeed.setText(Double.toString(Double.parseDouble(variableValues)));
}

#2

Dear @HULK,

Please, reference to the Ubidots REST API Reference to learn how to build the Get Request to the Ubidots Server.

For more information you can reference to the Ubidots Academy.

All the best,
Maria C.


#3

Android textview with android studio

<TextView
android:id="@+id/firstTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WelookupsAndroid"
android:textSize="25sp"
android:gravity="center_horizontal"/> <!--center horizontal gravity-->