# Retrieve data from ANTARES using Android

#### Prerequisites

* Java JDK. Not installed yet? [Follow this tutorial](https://www.oracle.com/java/technologies/downloads/#java8).
* Android Studio IDE. Not installed yet? [Follow this tutorial](https://developer.android.com/studio/index.html).

#### **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](https://antares.id/assets/files/libraries/antares-http-api-0.9.jar)
2. Klik "Mulai Aplikasi Android Studio baru".\ <br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/qlOLlOAKtkRskOvGNcuJ/image.png" alt=""><figcaption></figcaption></figure>
3. Give your app a name.<br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/I5kLcwA9Wn20sYqQdOXc/image.png" alt=""><figcaption></figcaption></figure>
4. Define the target SDK.<br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/LlMdJSdqf1m5hr34Ywh1/image.png" alt=""><figcaption></figcaption></figure>
5. Define your Activity. We recommend you to choose "Empty Activity".<br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/sRqaucBim2mioS6Q26vE/image.png" alt=""><figcaption></figcaption></figure>
6. Name your Activity. We recommend you to choose the default option "MainActivity".

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/u8Qeg5Mf6F7270jHOlHi/image.png" alt=""><figcaption></figcaption></figure>
7. Click on the "Android" section.<br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/dx3nkJOMmJ5fI3aKP3t4/image.png" alt=""><figcaption></figcaption></figure>
8. Move the layout to "Project".<br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/UtrLW82ddgKjXPzLv7gy/image.png" alt=""><figcaption></figcaption></figure>
9. Copy-Paste the downloaded library jar into app -> libs.

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/T1EFECSc7F5BQD6oudhV/image.png" alt=""><figcaption></figcaption></figure>
10. Congratulations! You have inserted the library jar.<br>

    <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/x1oNeCgNVplyPoxHX36w/image.png" alt=""><figcaption></figcaption></figure>

### **Source Code**

**Data stored in Antares**

<figure><img src="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2Fw6HFcvy3xX58Kfi6G7cn%2Fimage.png?alt=media&#x26;token=27ceaad6-a6cb-4932-b7d2-0df8b6208b1c" alt=""><figcaption></figcaption></figure>

#### **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.

```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:

{% code lineNumbers="true" %}

```xml
<?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>
```

{% endcode %}

#### **MainActivity.java**

{% code lineNumbers="true" %}

```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();
        }
    }
}
```

{% endcode %}

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

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/xUtSaqHoOzPXpLE0GSsl/image.png" alt=""><figcaption></figcaption></figure>

### **Output**

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/hdUT5vYgCXXnMTMY0PEh/image.png" alt=""><figcaption></figcaption></figure>
