본문 바로가기

android tech.

Https 및 언어에 따라 Json 다운로드

public static String downloadUrl(String urlString, String locale) throws IOException {
        InputStream result = null;
        Writer writer = null;
        URL url = new URL(urlString);

        setTrust();

        HttpsURLConnection httpsConn = (HttpsURLConnection)url.openConnection();
        httpsConn.setHostnameVerifier(new HostnameVerifier() {
             @Override
             public boolean verify(String hostname, SSLSession session) {
                 return true;
             }
        });

        HttpURLConnection conn = httpsConn;
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(10000);
        conn.setRequestProperty("Connection", "keep-alive");

        if(locale.equals("none")) {
            conn.addRequestProperty("Accept-Language", "en-US");
        } else {
            if(locale.equals("ko")) {
                conn.addRequestProperty("Accept-Language", "ko-KR");
            } else if(locale.equals("en")) {
                conn.addRequestProperty("Accept-Language", "en-US");
            } else {
                conn.addRequestProperty("Accept-Language", "en-US");
            }
        }
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        conn.connect();

        result = conn.getInputStream();

        writer = new StringWriter();
        char[] buffer = new char[4094];
        try{
            Reader reader = new BufferedReader(new InputStreamReader(result, "UTF-8"));
            int n;
            while((n=reader.read(buffer)) != -1){
                writer.write(buffer, 0, n);
            }
        }

        finally {
           if(result != null) result.close();
        }

        return writer.toString();
    }

    private static void setTrust() {
        TrustManager[] list = new TrustManager[] {
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                }
        };

        try {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, list, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
    }




'android tech.' 카테고리의 다른 글

enum 활용  (0) 2016.10.06
파일 이어받기  (0) 2015.12.02
가속 센서와 자기 센서로 회전 구하기  (0) 2015.08.31
Beacon 연동  (0) 2015.08.31
Obb 파일 다운로드  (0) 2015.08.31