[SOLVED] Send data from Android Studio

Need to send data from android app to ubidots when a button is pressed.

Similar issue(but not resolved yet)
[SOLVED] How to send data to ubidots using a tooggle button on android studio?

Greetings, have you already tried to capture the press button and to trigger a callback function that sends data to Ubidots? You can find an example of a function for sending data using JAVA here.

All the best

LightOn = findViewById(R.id.buttonId_lightOn);
LightOff = findViewById(R.id.buttonId_lightOff);

LightOn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ApiClient apiClient = new ApiClient("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        Variable device1 = apiClient.getVariable("xxxxxxxxxxxxxxxxxxx");
        device1.saveValue(1);
    }
});

LightOff.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ApiClient apiClient = new ApiClient("xxxxxxxxxxxxxxxxxxx");
        Variable device1 = apiClient.getVariable("xxxxxxxxxxxxxxxxxxx");
        device1.saveValue(0);
    }
});

This is my java code, When i run this code the application crashes

I read in some post that the API is outdated. If so how to achieve the same using HTPP/MQTT request from android studio?

Greetings, the API is not oudated but the library yes, so my advise is to make the POST request using the previous Java example that I shared with you.

All the best

1 Like
public class MainActivity extends AppCompatActivity {

    Button LightOn;
    private static String deviceLabel = "xxx";
    private static String variableLabel = "device1";
    private static String token = "xxxx";
    private static String endpoint = "https://things.ubidots.com/api/v1.6/devices";
    private static String userAgent = "Java/0.1";

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

        LightOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

It would be much appreciated if you could help me to combine Java example with my code.
I tried a few ways but it didn’t work out.

I wrote some code to do simple http request. I its working with other link ( like when i try to gpio of NodeMCU http://192.168.1.4/LED=ON).

But when i replace that link with ubidots its not working.

Here is my code

public class MainActivity extends AppCompatActivity {

    Button LightOn, LightOff;
    TextView textView;
    String url ="https://industrial.ubidots.com/api/v1.6/devices/spatez/?token=xxxxxxxxxx&_method=post&device2=1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LightOn = findViewById(R.id.buttonIdLightOn);
        LightOff = findViewById(R.id.buttonIdLightOff);
        textView = findViewById(R.id.textView);

        LightOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Instantiate the RequestQueue.
                final RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// Request a string response from the provided URL.
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                // Display the first 500 characters of the response string.
//                                textView.setText(response);
                                queue.stop();
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
//                        textView.setText("Error");
                        error.printStackTrace();
                        queue.stop();
                    }
                });
// Add the request to the RequestQueue.
                queue.add(stringRequest);

            }
        });
    }
}

:v:
Solved, here is my code. If anyone is having a similar issue.

public class MainActivity extends AppCompatActivity
{
    private static final String TAG = MainActivity.class.getName() ;
    Button LightOn, LightOff;
    TextView textView;

    private  RequestQueue mRequestQueue;
    private  StringRequest stringRequest;

    private static String deviceLabel = "xxxxxxxxxxx";
    private static String variableLabel = "xxxxxxxxx";
    private static String token = "xxxxxxxxxxxxxxxx";
    private static String endpoint = "https://industrial.api.ubidots.com/api/v1.6/devices";
    private static String userAgent = "Java/0.1";
    private static String value = "1";
    private String url = "https://industrial.ubidots.com/api/v1.6/devices/xxxxxxx/?token=xxxxxxxxx&_method=post&xxxxxxxxx=1";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LightOn = findViewById(R.id.buttonIdLightOn);
        LightOff = findViewById(R.id.buttonIdLightOff);
        textView = findViewById(R.id.textView);

        LightOn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                sendRequestAndPrintResponse();
            }
        });
    }

    private void sendRequestAndPrintResponse()
    {
        mRequestQueue = Volley.newRequestQueue(this);
        stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response)
            {
                Log.i(TAG,"Response : " + response.toString());
            }
        },new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                Log.i(TAG,"Error" + error.toString());
            }
        });
        mRequestQueue.add(stringRequest);
    }
}
2 Likes

Thanks for sharing your solution with the community, have a nice rest of your day!