[SOLVED] Getting data from ubidots using android studio

I’m new in the environment of ubidots and android studio. i need a simple java codes that can get data from the ubidots using and android application, android studio only thank you. can you give me a whole project so that i can explore every detail.

Sure!

There’s a tutorial made by Ubidots for Android (and they use Android Studio, too). But they just send the value.

http://ubidots.com/docs/devices/android.html

The change is pretty easy, you just have to change this part of the code:

public class ApiUbidots extends AsyncTask<Integer, Void, Void> {
    private final String API_KEY = "our_api_key";
    private final String VARIABLE_ID = "our_variable_id";

    @Override
    protected Void doInBackground(Integer... params) {
        ApiClient apiClient = new ApiClient(API_KEY);
        Variable batteryLevel = apiClient.getVariable(VARIABLE_ID);

        batteryLevel.saveValue(params[0]);
        return null;
    }
}

To something like this:

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

    @Override
    protected Values[] 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) {
        // Update your views here
    }
}

You can also take a look at the library documentation at GitHub: https://github.com/ubidots/ubidots-java

1 Like

Did you import all the classes from the Ubidots Java package to your project? That is, in the first lines you should have this line:

import com.ubidots.*

BTW, you should take out that image because you’re exposing your API key.

Is this correct?

Yes, you can also do that (importing specific classes) and it should work.


Did you see my error? :joy:

Please change the Values that your IDE highlighted in red for Value. It’s a class, then it’s in singular and I wrote it in plural.

I’m going to fix my post above.

Sorry for the typo.

How can i test it, if its working?. should i use two android phone that send data and retrieve data in real time or i can retrieve data without real time sendding of data?.

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.