ကျနော့်ရဲ့ API ကို Android Application ရေးတဲ့အခါ အလွယ်ဆုံး ဘယ်လိုအသုံးပြုရမလဲဆိုတာကို ဒီမှာ ရှင်းပြပေးပါမယ်။ မိတ်ဆွေအနေနဲ့ Android Programming ကို ကျွမ်းကျွမ်းကျင်ကျင် အသုံးပြုတတ်မယ်ဆိုရင် မိမိဘာသာ စိတ်ကြိုက်တိုးချဲ့ပြီး ဖန်တီးကြည့်ပါ။ ဒီမှာကတော့ API အသုံးပြုတတ်ရုံလောက်ကိုပဲ ပြောပြပေးမှာပါ။ ပထမဆုံး AndroidManifest ဖိုင်ထဲမှာ Internet Permission ကို အောက်ပါအတိုင်း ထည့်ပေးပါ။ ကျနော်တို့အနေနဲ့ Online Api ကို အသုံးပြုမှာဖြစ်တဲ့အတွက် Internet ခွင့်ပြုချက်လိုအပ်ပါတယ်။

AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

ပြီးရင် UI အတွက် မိမိဘာသာ စိတ်ကြိုက်တည်ဆောက်ပါ။ ဒီမှာတော့ အခြေခံကုဒ်လေးပဲ ရေးပြပါမယ်။ API သင်ခန်းစာဖြစ်တဲ့အတွက် အကျယ်တဝင့် မရှင်းပြချင်လို့ပါ။

main.xml:

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

      <TextView
        android:id="@+id/tv"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    </LinearLayout>

ပြီးတော့ နောက်တဆင့်အနေနဲ့ API ကို အသုံးပြုမှာဖြစ်တဲ့အတွက် http request လုပ်ပါမယ်။ ဒီနေရာမှာ အသုံးပြုနိုင်တဲ့နည်းလမ်းပေါင်း စုံရှိတတ်ပေမယ့် ကျနော်ကတော့ HttpURLConnection နည်းလမ်းကိုပဲ အသုံးပြုပါမယ်။ ပြီးတော့ သင်ခန်းစာတရပ်အနေနဲ့ သင်ပြနေရရင် အရမ်းကြာနေမှာစိုးလို့ အသင့်သုံး Class လေးတစ်ခု ဖန်တီးပေးလိုက်ပါတယ်။ အဲဒီဟာလေးကို ဘာမှ ပြင်မနေနဲ့ မိမိရဲ့ Project ထဲမှာ ထည့်သုံးနိုင်ပါတယ်။ လောလောဆယ် တွင်တွင်ကျယ်ကျယ် မသုံးတတ်သေးဘူးဆိုရင် RequestJson.java လို့ နာမည်ပေးပြီး MainActivity.java နဲ့ တူတူထားလိုက်ပါ။ RequestJson.java ထဲမှာ အောက်က ကုဒ်တွေကို အကုန်ကူးထည့်လိုက်ပါ။

RequestJson.java:

import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.widget.TextView;
import org.json.JSONObject;
import org.json.JSONException;


public class RequestJson extends AsyncTask {
    private final String apiUrl,param;
    private final TextView textView;


    public RequestJson(String apiUrl, String param, TextView textView) {
        this.apiUrl = apiUrl;
        this.param = param;
        this.textView = textView;
    }

    @Override
    protected String doInBackground(Void... voids) {
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;
            StringBuilder responseBuilder = new StringBuilder();
            while ((output = br.readLine()) != null) {
                responseBuilder.append(output);
            }

            conn.disconnect();

            return responseBuilder.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getKeyFromJson(String jsonResponse) {
        try {
            JSONObject jsonObject = new JSONObject(jsonResponse);
            String key = jsonObject.getString(param);
            return key;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

	@Override
    protected void onPostExecute(String jsonResponse) {
        super.onPostExecute(jsonResponse);
        if (jsonResponse != null) {
            String key = getKeyFromJson(jsonResponse);
            if (key != null) {
                textView.setText(key);
            }
        }
    }
}

ဒီအဆင့်ထိရပြီဆိုရင် တော့ MainActivity.java ဖိုင်မှာ အောက်ကလို နမူနာ တည်ဆောက်နိုင်ပါပြီ။

MainActivity.java:

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;


public class MainActivity extends Activity {
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textView = findViewById(R.id.tv);

        String url = "https://api.coder-guru.com/v1/calendar/PeW725u71ed9853f97d5d1LOieca3f7/1985-01-07";
		RequestJson apiExample = new RequestJson(url,"mm_date_str",textView);
		apiExample.execute();
		
    }
	
}

ဒီနမူနာကုဒ်က API ရဲ့ Calendar ကို API Key နဲ့ ရက်စွဲနမူနာထည့်ပြီး မြန်မာရက်စွဲ အဖြစ် ပြောင်းလဲပေးဖို့ mm_date_str ကို လှမ်းခေါ်လိုက်တာပါ။ ဒါဆိုရင် Output က အောက်ပါအတိုင်း ထွက်ပေါ်လာပါလိမ့်မယ်။

Output:
  
၁၃၄၆ ခုနှစ်၊ ပြာသို လပြည့်ကျော် ၂ ရက်၊ တနင်္လာနေ့။
  

လက်တွေ့လုပ်ကြည့်ရင် ပိုပြီးနားလည်မှာပါ။ ကျန်တဲ့ Json Key တွေကိုလည်း "mm_date_str" နေရာမှာ အစားထိုးပြီး ခေါ်သုံးကြည့်ပါ။ ဆက်ပြီး မိမိဉာဏ်ရှိသလို သုံးစွဲနိုင်ပါတယ်။ API Key အနေနဲ့ ခေါ်သုံးလို့ရတဲ့ Parameter တွေအကြောင်းကို API Documentation မှာ ဖတ်ကြည့်ပါ။