GetAreaCode.java 10.3 KB
package com.example.test;

import android.content.ContentValues;
import android.os.StrictMode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


public class GetAreaCode extends Thread {

    MainActivity mContext;

    public GetAreaCode(MainActivity mContext)
    {
        this.mContext = mContext;
    }

    // 시 리스트 받아오는 함수
    public LinkedList GetTopCode(){

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        String result;

        URL url = null;

        URLConnection conn;

        JSONParser parser;
        JSONArray jArr;
        JSONObject jobj;

        //시 검색
        try {
            url = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/top.json.txt");
            conn = url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        LinkedList mArea = new LinkedList();

        try {
            InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            result = br.readLine().toString();
            br.close();

            parser = new JSONParser();
            jArr = (JSONArray) parser.parse(result);

            for (int i = 0; i < jArr.size(); i++) {
                jobj = (JSONObject) jArr.get(i);
                mArea.add(jobj.get("value"));
            }

        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

        return mArea;
    }

    // 시 코드 받아와서 구 검색하는 함수
    public LinkedList GetMdlCode(String Top){
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        String result;
        String areaMdl = "종로구";
        String code = "";    //지역 코드

        URL url = null;

        URLConnection conn;

        JSONParser parser;
        JSONArray jArr;
        JSONObject jobj;

        //시 검색
        try {
            url = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/mdl."+Top+".json.txt");
            conn = url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        LinkedList mArea = new LinkedList();

        try {
            InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            result = br.readLine().toString();
            br.close();

            parser = new JSONParser();
            jArr = (JSONArray) parser.parse(result);

            for (int i = 0; i < jArr.size(); i++) {
                jobj = (JSONObject) jArr.get(i);

                mArea.add(jobj.get("value"));

                if (jobj.get("value").equals(areaMdl)) {
                    code = (String) jobj.get("code");
                }
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

        return mArea;
    }

    // 구 코드 받아와서 동 검색 함수
    public LinkedList GetLeafCode(String Mdl){
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        String result;
        String areaLeaf = "종로1가동";
        String code = "";

        URL url = null;
        URLConnection conn;

        JSONParser parser;
        JSONArray jArr;
        JSONObject jobj;

        //시 검색
        try {
            url = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/leaf."+Mdl+".json.txt");
            conn = url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        LinkedList mArea = new LinkedList();

        try {
            InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            result = br.readLine().toString();
            br.close();

            parser = new JSONParser();
            jArr = (JSONArray) parser.parse(result);

            for (int i = 0; i < jArr.size(); i++) {
                jobj = (JSONObject) jArr.get(i);

                mArea.add(jobj.get("value"));

                if (jobj.get("value").equals(areaLeaf)) {
                    code = (String) jobj.get("code");
                }
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

        return mArea;
    }

    // 시 코드 검색하는 함수
    public String SearchTop(Object areaTop) {
        //areaTop은 시 이름!!! (코드가 아님)
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        String result;
        String code = "";    //지역 코드

        URL url = null;
        URLConnection conn;

        JSONParser parser;
        JSONArray jArr;
        JSONObject jobj;

        //시 검색
        try {
            url = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/top.json.txt");

            conn = url.openConnection();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            result = br.readLine().toString();
            br.close();

            parser = new JSONParser();
            jArr = (JSONArray) parser.parse(result);

            for (int i = 0; i < jArr.size(); i++) {
                jobj = (JSONObject) jArr.get(i);

                if (jobj.get("value").equals(areaTop)) {
                    code = (String) jobj.get("code");
                    break;
                }
            }

        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

        return code;
    }

    // 구 코드 검색하는 함수
    public String SearchMdl(Object areaMdl, Object Top) {
        // areaMdl은 구 이름!! 코드가 아님
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        String result;
        String code = "";    //지역 코드

        URL url = null;
        URLConnection conn;

        JSONParser parser;
        JSONArray jArr;
        JSONObject jobj;

        //시 검색
        try {
            url = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/mdl."+Top+".json.txt");
            conn = url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            result = br.readLine().toString();
            br.close();

            parser = new JSONParser();
            jArr = (JSONArray) parser.parse(result);

            for (int i = 0; i < jArr.size(); i++) {
                jobj = (JSONObject) jArr.get(i);

                if (jobj.get("value").equals(areaMdl)) {
                    code = (String) jobj.get("code");
                    break;
                }
            }

        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

        return code;
    }


    // 동 검색하는 함수
    public String SearchLeaf(Object areaLeaf, Object Mdl) {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        String result;
        String code = "";    //지역 코드

        URL url = null;
        URLConnection conn;

        JSONParser parser;
        JSONArray jArr;
        JSONObject jobj;

        //시 검색
        try {
            url = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/leaf."+Mdl+".json.txt");
            conn = url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            InputStreamReader isr = new InputStreamReader(url.openStream(), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            result = br.readLine().toString();
            br.close();

            parser = new JSONParser();
            jArr = (JSONArray) parser.parse(result);

            for (int i = 0; i < jArr.size(); i++) {
                jobj = (JSONObject) jArr.get(i);

                if (jobj.get("value").equals(areaLeaf)) {
                    code = (String) jobj.get("code");
                    break;
                }
            }

        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

        return code;
    }


    @Override
    public void run() {
        super.run();
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
    }
}