package com.example.junsang.simsimi; import android.annotation.TargetApi; import android.os.AsyncTask; import android.os.Build; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends ActionBarActivity { private static final String LOG_TAG = "junsang"; int flag = 0; //url flag // public String parse_pm(String str) { String parse_1 = str.split("")[1]; String parse_2 = parse_1.split("")[0]; return parse_2; } private static final String URL = "http://jp3.aqicn.org/aqicn/services/geolocate/?autolocate&n=1"; String nearestPos = ""; String results; TextView pm_tv; @TargetApi(Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pm_tv = (TextView) findViewById(R.id.pm_tv); new DownloadPageTask().execute(URL); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private class DownloadPageTask extends AsyncTask { @Override protected String doInBackground(String... urls) { try { return getHtmlFromHttp(urls[0]); } catch (IOException e) { return "Unable to retrieve web page."; } } @Override protected void onPostExecute(String result) { results = result; String nearestUrl = ""; JSONArray jArr = null; if(flag==0) { try { jArr = new JSONArray(result); for (int i = 0; i < jArr.length(); i++) { JSONObject curl = jArr.getJSONObject(i); nearestPos = curl.getString("name"); nearestUrl = curl.getString("curl"); } } catch (JSONException e) { e.printStackTrace(); } System.out.println(nearestPos); System.out.println(nearestUrl); flag++; new DownloadPageTask().execute(nearestUrl); } else { results = result; String pm25 =""; pm25 = parse_pm(result); System.out.println(nearestPos); pm_tv.setText("지금 " + nearestPos + "의 초미세먼지 농도는 " + pm25 + "㎍/㎥야~"); flag =0; } } } private String getHtmlFromHttp(String myUrl) throws IOException { InputStream is = null; String str = ""; try { URL url = new URL(myUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(10000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); int resCode = conn.getResponseCode(); if (resCode == HttpURLConnection.HTTP_OK) { StringBuffer sb = new StringBuffer(); is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { sb.append(line); } str = sb.toString(); } else { conn.disconnect(); } } finally { if (is != null) { is.close(); } } return str; } }