๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ์ธ ๊ณต๋ถ€/Android

[Android]๊ณต๊ณต๋ฐ์ดํ„ฐ '๋ฏผ๋ฐฉ์œ„ ๋Œ€ํ”ผ์†Œ์ •๋ณด ์„œ๋น„์Šค' API ํŒŒ์‹ฑํ•˜๊ธฐ

728x90
๋ฐ˜์‘ํ˜•
 

[Android] T-Map API์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง€๋„ ๋„์›Œ๋ณด๊ธฐ

[๐Ÿ‘จ‍๐ŸŽ“ํ•™๊ต ์ˆ˜์—…๐Ÿ‘จ‍๐ŸŽ“/[3-2]Android] - [Android] ์ž๋ฐ”๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๋‹ค์Œ ์ง€๋„ API์‚ฌ์šฉํ•˜์—ฌ ์ง€๋„ ๋„์›Œ๋ณด๊ธฐ [Android] ์ž๋ฐ”๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๋‹ค์Œ ์ง€๋„ API์‚ฌ์šฉํ•˜์—ฌ ์ง€๋„ ๋„์›Œ๋ณด๊ธฐ ๋‹ค์Œ์ง€๋„ API ํ‚ค ๋ฐœ..

sungmin.dev

 

[Android] ์ž๋ฐ”๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๋‹ค์Œ ์ง€๋„ API์‚ฌ์šฉํ•˜์—ฌ ์ง€๋„ ๋„์›Œ๋ณด๊ธฐ

๋‹ค์Œ์ง€๋„ API ํ‚ค ๋ฐœ๊ธ‰ 1. ์ง€๋„ API์„ ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” Kakao Developers(https://developers.kakao.com/)์‚ฌ์ดํŠธ์— ๋ฐฉ๋ฌธํ•˜์—ฌ ๊ฐœ๋ฐœ์ž ๋“ฑ๋ก์„ ํ•œํ›„ ์•ฑ ๋“ฑ๋ก์„ ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. 2. ์•ฑ SDK์„ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด์„œ๋Š” ํ”Œ๋žซํผ..

sungmin.dev

์ตœ๊ทผ ๊ณต๊ณต๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•œ ์•ฑ ๊ฒฝ์ง„๋Œ€ํšŒ ๋ฐ ์•„์ด๋””์–ด ๊ฒฝ์ง„๋Œ€ํšŒ๊ฐ€ ๋งŽ์ด ๊ฐœ์ตœ๋˜๊ณ , ๊ฐ ์‹œ๋„์ฒญ์—์„œ๋„ ๊ณต๊ณต๋ฐ์ดํ„ฐ๋ฅผ ๋งŽ์ด ํ™œ์„ฑํ™” ํ•˜๊ธฐ ์œ„ํ•ด ๊ณต๊ณต๋ฐ์ดํ„ฐ๋ฅผ ๊ฐœ๋ฐฉํ•˜๊ณ  ์žˆ๋‹ค.

ํ˜„์žฌ ๋‚˜ ์—ญ์‹œ๋„  ์ถฉ๋‚จ๋„์ฒญ ๊ณต๊ณต๋ฐ์ดํ„ฐ ์„œํฌํ„ฐ์ฆˆ 1๊ธฐ๋กœ ํ™œ๋™์„ ํ•˜๋ฉด์„œ 2019๋…„ 3ํ•™๋…„ 2ํ•™๊ธฐ ์บก์Šคํ†ค๋””์ž์ธ ๊ณผ๋ชฉ์ธ ๋ชจ๋ฐ”์ผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณผ๋ชฉ์—
๊ณต๊ณต๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ–‰์ •์•ˆ์ „๋ถ€(์ดํ•˜ ํ–‰์•ˆ๋ถ€)์—์„œ ๊ณต๊ฐœํ•œ '๋ฏผ๋ฐฉ์œ„ ๋Œ€ํ”ผ์†Œ์ •๋ณด ์„œ๋น„์Šค' ๋ผ๋Š” ๊ณต๊ณต๋ฐ์ดํ„ฐ๋ฅผ Android์— ํŒŒ์‹ฑํ•˜๊ธฐ ์œ„ํ•ด ๊ฒ€์ƒ‰๋„ ๋งŽ์ด ํ•ด๋ณด๊ณ  2์ฃผ๊ฐ„ ์‚ฝ์งˆํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ธ€์„ ์“ฐ๋ ค๊ณ  ํ•œ๋‹ค.

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋Š” Kotlin ์–ธ์–ด๊ฐ€ ์•„๋‹Œ Java ๊ธฐ๋ฐ˜์ž„์„ ๋จผ์ € ๋ฐํžˆ๊ณ  ์ž‘์„ฑ์„ ํ•œ๋‹ค.

์šฐ์„  ๊ณต๊ณต๋ฐ์ดํ„ฐ๋Š” ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ์— ์ ‘์†ํ•˜์—ฌ ์‹ ์ฒญํ•˜๊ณ , ๋งˆ์ดํŽ˜์ด์ง€์—์„œ APP KEY๋“ฑ ๊ธฐ์ˆ  ๋ฌธ์„œ๋“ฑ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ณต๊ณต๋ฐ์ดํ„ฐ ์‹ ์ฒญ ํ™”๋ฉด

 

* ํ•ด๋‹น ๊ธฐ์ˆ  ๋ฌธ์„œ๋Š” ์ตœ์‹ ํ™”๊ฐ€ ์•ˆ๋˜์–ด ์žˆ์–ด์„œ End Point๋Š” ์‹ ์ฒญํ™”๋ฉด์— ๋‚˜์˜ค๋Š” ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํŒŒ์‹ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์•ฑ ์‹ ์ฒญ์ด ์™„๋ฃŒ ๋˜๋ฉด ์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑ ํ•œํ›„ 

AndroidManifest.xml ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์€ ์ธํ„ฐ๋„ท ๊ถŒํ•œ์„ ๋„ฃ์–ด์ค€๋‹ค.

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

 

๊ทธ ์ดํ›„ activavity_main.xml ํŒŒ์ผ์— ๋ถˆ๋Ÿฌ์˜จ API ํ…์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด TextView์„ ๋„ฃ์–ด์ค€๋‹ค.

 <TextView
 	android:id="@+id/result"
	android:layout_width="match_parent"
	android:layout_height="match_parent"/>

 

๊ทธ ์ดํ›„ MainActivity.java ํŒŒ์ผ์„ ์—ด์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ํŒŒ์‹ฑ์ด ๋˜๋Š”๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

package dev.sungmin;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    String AppKey = "API Key"; //๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ์—์„œ ๋ถ€์—ฌ๋ฐ›์€ API ํ‚ค

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

        TextView status1 = (TextView) findViewById(R.id.result); //ํŒŒ์‹ฑ๋œ ๊ฒฐ๊ณผํ™•์ธ!
        BufferedReader br = null;
        try{
            String urlstr = "http://apis.data.go.kr/1741000/CivilDefenseShelter2/getCivilDefenseShelterList?ServiceKey=" + AppKey;
            URL url = new URL(urlstr);
            HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection();
            urlconnection.setRequestMethod("GET");
            br = new BufferedReader(new InputStreamReader(urlconnection.getInputStream(),"UTF-8"));
            String result = "";
            String line;
            while((line = br.readLine()) != null) {
                result = result + line + "\n";
            }
            status1.setText(result);
            System.out.println(result);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}

 

ํŒŒ์‹ฑ๋œ ๊ฒฐ๊ณผ

728x90
๋ฐ˜์‘ํ˜•