[SOLVED] Getting data from ubidots using android studio

In the postExecute method you’ll receive the values from your variables, look at the arguments of that method. Then, you can update your views with those values.

How can I update the views using this code that the ubidots provide?, can you give me the code plss?. thank you :3

You can see examples of how to use AsyncTasks in the Android Developers documentation site:
http://developer.android.com/intl/zh-tw/reference/android/os/AsyncTask.html

You just have to change their way of manipulating data to yours and use the values you get from Ubidots.

Hello. We are still waiting for the code correction. Thanks

Just wanted to ask; from the UbiDot Battery app that we are editing, do you still need the broadcast receiver to get data from UbiDot?

Hi 5ran5.

The code was corrected immediately after I wrote that post. As you can see in that message I explain that in the argument list I wrote Values[] (in plural) instead of Value[] (in singular).

You can use the code above without any compiling problems.

Regards

1 Like

No, you don’t need the Broadcast Receiver to get data from Ubidots. The Broadcast Receiver is just to keep checking the battery level and receive updates from it when the value changes.

Regards

1 Like

public class ApiUbidots extends AsyncTask<Integer, Void, Value[]> {
@Override
protected Value[] doInBackground(Integer… params) {
ApiClient apiClient = new ApiClient(API_KEY);
Variable batteryLevel = apiClient.getVariable(VARIABLE_ID);
Value[] variableValues = batteryLevel.getValues();

       return variableValues;
    }

    @Override
    protected void onPostExecute(Value[] variableValues) {
       TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(variableValues[0].toString());


    }

I want to get the last value of the variable from ubidots using Android
I used the above code to display the value ina textbox but i get the output as shown in image

Kindly help me to get the last variable value

Thank Your

hello, you finally can changed that data to whole number ?, i have the same problem

You’re printing the textual representation of that Value instance in your TextView.

To show the numeric value that the instance holds, you should change this line:

textView.setText(variableValues[0].toString());

To this:

textView.setText(variableValues[0].getValue());

Regards

i already do that but i show me and error i have this

@Override
        protected void onPostExecute(Value[] variableValues) {
            // Update your views here
            TextView textView = (TextView) findViewById(dato);
            textView.setText(variableValues[0].getValue());


        }

the error that show me is

Then the value should be casted to a String because that’s the type of what setText() accepts as parameter.

Double variableValue = variableValues[0].getValue();
textView.setText(String.valueOf(variableValue));

Regards

works perfectly thanks a lot

I’m also working on android application but i want to get data from ubidots using Device Label (as i have multiple devices) and Variable Label please help me to solve my problem.

At this very moment the Java Library only let’s you get information from the Devices and Variables only using IDs.

I think a workaround for this would be that after you obtain the information using the library just make a filter for the Devices and Variables and return only what you need.

Here’s a link to StackOverflow where someone answered how to do that filtering in Java. If you’re using Android Studio 3.0, you don’t need to do anything extra to activate the Java 8 features, but if you’re not using Android Studio 3.0 then you have to activate the Jack toolchain, but remember that Jack is now being deprecated.

I’m not getting that. I don’t know how to apply filter on Ubidots java library… Should i use REST API (HTTP request) to get the device and variables by their label?

The filter would not be applied on top of the library but in its results, i.e. the array of Devices/Variables you receive.

You can use the HTTP API to get Variables by their label, there’s no way to get Devices by their label, yet.

would you please suggest me an easy way to solve my problem… i have created multiple devices using the mac address and each have 3 variables i just want to get the data of particular device variables using device label so that the user with unique device label (mac address) can get his own data.

I’m waiting for your reply

i try that code but unfortunately close app . and data 0 .
i am new about android studio and the code.