public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button_call);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v){
Toast.makeText(getApplicationContext(),"미세먼지 조회", Toast.LENGTH_SHORT).show();
EditText et_webpage_src = (EditText) findViewById(R.id.webpage_src);
URL url ;
HttpURLConnection urlConnection = null;
BufferedInputStream buf = null;
try{
url= new URL("http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnMesureSidoLIst?serviceKey=nlioVxDR662tsiIWuR5YKgacitpj6B%2FEqqFGzdDhqrd%2FIHv5wTLOSMGnLRh2UKeP3NMkH5Os5r9gg5ry5sTj1g%3D%3D&numOfRows=10&pageNo=1&sidoName=%EC%84%9C%EC%9A%B8&_returnType=json&searchCondition=DAILY");
urlConnection = (HttpURLConnection) url.openConnection();
BufferedReader bufreader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
Log.d("line:",bufreader.toString());
String line ;
String page = "";
while((line = bufreader.readLine())!=null){
Log.d("line:",line);
page +=line;
}
JSONObject json = new JSONObject(page);
JSONArray jArr = json.getJSONArray("item");
for (int i=0; i<jArr.length(); i++){
json = jArr.getJSONObject(i);
String cityName = json.getString("cityName");
String coValue = json.getString("coValue");
String dataTime = json.getString("dataTime");
String no2Value = json.getString("no2Value");
String o3Value = json.getString("o3Value");
String pm10Value = json.getString("pm10Value");
String pm25Value = json.getString("pm25Value");
String so2Value = json.getString("so2Value");
System.out.println("cityName:"+cityName+"/coValue:"+coValue+"/dataTime"
+dataTime+"/no2Value"+no2Value+"/o3Value"+o3Value+"/pm10Value"+pm10Value+"/pm25Value"+pm10Value+"so2Value"+so2Value);
et_webpage_src.append("[ "+cityName+" ]\n");
et_webpage_src.append("[ "+coValue+" ]\n");
et_webpage_src.append("[ "+dataTime+" ]\n");
et_webpage_src.append("[ "+no2Value+" ]\n");
et_webpage_src.append("[ "+o3Value+" ]\n");
et_webpage_src.append("[ "+pm10Value+" ]\n");
et_webpage_src.append("[ "+pm25Value+" ]\n");
et_webpage_src.append("[ "+so2Value+" ]\n");
}
} catch (Exception e) {
et_webpage_src.setText(e.getMessage());
}finally{
urlConnection.disconnect();
}
try {
System.out.printf("i = %s \n");
} catch (NullPointerException npe) {
System.out.println(" Null Opint Exception!!");
}
}
}