[SOLVED] Getting data from ubidots using android studio


#1

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.


[SOLVED] I want to get multiple data from the ubidots using android app
#2

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


#5

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.


#6

Is this correct?


#7

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.


#8

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?.


#9

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.


#10

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


#11

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.


#13

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


#14

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?


#15

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


#16

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


#17

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


#18

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


#19

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


#20

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


#21

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


#22

works perfectly thanks a lot


#23

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.