Retrieve data from ANTARES using Android

Prerequisites

Create Android Application

  1. Download the Antares Library for Android by clicking the button below. Save the .jar library to your computer's local storage. DOWNLOAD

  2. Klik "Mulai Aplikasi Android Studio baru".

  3. Give your app a name.

  4. Define the target SDK.

  5. Define your Activity. We recommend you to choose "Empty Activity".

  6. Name your Activity. We recommend you to choose the default option "MainActivity".

  7. Click on the "Android" section.

  8. Move the layout to "Project".

  9. Copy-Paste the downloaded library jar into app -> libs.

  10. Congratulations! You have inserted the library jar.

Source Code

Data stored in Antares

AndroidManifest.xml

In order to execute the Antares API, we need Internet access and in order to access the Internet on Android, we need to add the following lines to AndroidManifest.xml.

<!--- IMPORTANT!!!!!!! --->
<!--- Tambahkan line berikut di AndroidManifest.xml Anda --->
<!--- Setelah manifest --->
<uses-permission android:name="android.permission.INTERNET" />
<!--- Sebelum application --->XMLCopy

activity_main.xml

Modify ALL the contents of activity_main.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No Data"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/btnRefresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Refresh"/>

</android.widget.LinearLayout>

MainActivity.java

// !!!!------ IMPORTANT ---------!!!!//
// Ubah semua code setelah package nama.package.Anda dengan
// dengan Code berikut

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import id.co.telkom.iot.AntaresHTTPAPI;
import id.co.telkom.iot.AntaresResponse;

public class MainActivity extends AppCompatActivity implements AntaresHTTPAPI.OnResponseListener{

    private Button btnRefresh;
    private TextView txtData;
    private String TAG = "ANTARES-API";
    private AntaresHTTPAPI antaresAPIHTTP;
    private String dataDevice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // --- Inisialisasi UI yang digunakan di aplikasi --- //
        btnRefresh = (Button) findViewById(R.id.btnRefresh);
        txtData = (TextView) findViewById(R.id.txtData);

        // --- Inisialisasi API Antares --- //
        antaresAPIHTTP = AntaresHTTPAPI.getInstance();
        antaresAPIHTTP.addListener(this);

        btnRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antaresAPIHTTP.getLastDataofDevice("your-access-key","your-application-name","your-device-name");
            }
        });
    }

    @Override
    public void onResponse(AntaresResponse antaresResponse) {
        // --- Cetak hasil yang didapat dari ANTARES ke System Log --- //
        Log.d(TAG,antaresResponse.toString());

        try {
            JSONObject body = new JSONObject(antaresResponse.getBody());
            dataDevice = body.getJSONObject("m2m:cin").getString("con");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    txtData.setText(dataDevice);
                }
            });
            Log.d(TAG,dataDevice);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

Logcat Results

We can filter the output log results on Android. Please fill in the section as shown below. When the button is clicked, the output results can also be seen in the image below.

Output

Last updated