[SOLVED] Android: get multiple variables from Ubidots

Based on examples I managed already to retrieve data of one variable from Ubidots, here weight. However, I need to retrieve about 10 Variables from an Ubidots datasource.

Since I do have zero experience with Java programming, I would really appreciate if someone could adapt below single variable example to a multiple variables example.

package de.httpName.temp;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.ubidots.*;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new ApiUbidots().execute(15);
}
@Override
protected void onStart() {
    super.onStart();
}
@Override
protected void onStop() {
     super.onStop();
}
public class ApiUbidots extends AsyncTask<Integer, Void, com.ubidots.Value[]> {
    private final String API_KEY = "a23234234234";
    private final String VARIABLE_ID = "b24234234234";
    @Override
    protected com.ubidots.Value[] doInBackground(Integer... params) {
        ApiClient apiClient = new ApiClient(API_KEY);
        Variable weight = apiClient.getVariable(VARIABLE_ID);
        com.ubidots.Value[] variableValues = weight.getValues();
        return variableValues;
    }
    @Override
    protected void onPostExecute(com.ubidots.Value[] variableValues) {
        TextView textViewW = (TextView) findViewById(R.id.weight_val);
        TextView textViewT = (TextView) findViewById(R.id.weight);

        Double variableValueW = variableValues[0].getValue();
        textViewW.setText(String.valueOf(String.format("%.3f",variableValueW/1000))+" kg");
        Long variableValueT = variableValues[0].getTimestamp();
        String date = new java.text.SimpleDateFormat("dd. MMM yyyy HH:mm").format(new java.util.Date (variableValueT));
        textViewT.setText(String.valueOf(date));
 
    }
}

}

Greetings sir, simply replicate your actual piece of code to retrieve more values, i.e for two values it should be something like this:

private final String API_KEY = "a23234234234";
        private final String VARIABLE_ID = "b24234234234";
        private final String VARIABLE_ID_2 = "a23154234234";

        @Override
        protected com.ubidots.Value[] doInBackground(Integer... params) {
            ApiClient apiClient = new ApiClient(API_KEY);

            Variable weight = apiClient.getVariable(VARIABLE_ID);
            Variable weight2 = apiClient.getVariable(VARIABLE_ID_2);
            com.ubidots.Value[] variableValues = weight.getValues();
            com.ubidots.Value[] variable_2_Values = weight2.getValues();

Regards

Dear @jotathebest,

Thanks a lot for your answer.

Indeed, what you proposed was the first thing I did. My issue is here the Java syntax and not to call an Ubidots variable another time.

As per your example, one would need now to return besides variableValues also variable_2_Values, like indicated in below snippet. They way I indicated doesn’t work in Java, so I am looking for a way to access variableValues and variable_2_Values as global variables. I was trying already to define a global variable and assign the Ubidots values to that, but it seems Java requires to do that via the return at the end of the function.

        Variable weight = apiClient.getVariable(VARIABLE_ID);
        Variable weight2 = apiClient.getVariable(VARIABLE_ID_2);
        com.ubidots.Value[] variableValues = weight.getValues();
        com.ubidots.Value[] variable_2_Values = weight2.getValues();
     return variableValues variable_2_Values;

Hello!

Something similar involving the get of multiple Variables was solved in this thread, take a look at it: http://community.ubidots.com/t/solved-cant-controlling-switch-from-android/1051

Regards!