728x90
# 실행 화면
# AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.ac.skuniv.air_parsing">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="air_parsing"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
# activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
# MainActivity.java
package kr.ac.skuniv.air_parsing;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("2018305065_전유정");
tv = (TextView) findViewById(R.id.data);
String serviceUrl =
"http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty";
String serviceKey =
"BmggWicZAv3%2BOQxar9xrI6hNlsn9%2B%2B13cJPg0FRtn%2BBQ2HThaGn9qrYfb%2FTCSk%2FQGikZVrvxlWEvaamMaR%2FF9w%3D%3D";
String sidoName = "서울";
String pageNo = "1";
String numOfRows = "10";
String ver = "1.3";
String strUrl =
serviceUrl+"?sidoName="+sidoName+"&pageNo="+pageNo+"&numOfRows="+numOfRows+"&ServiceKey="+serviceKey+"&ver="+ver;
new DownloadWebpageTask().execute(strUrl);
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return (String)downloadUrl((String)urls[0]);
} catch (IOException e) {
return "다운로드 실패";
}
}
protected void onPostExecute(String result) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(result));
int eventType = xpp.getEventType();
String resultCode = "";
String stationName = "";
String pm10Value = "";
boolean bSet_resultCode = false;
boolean bSet_stationName = false;
boolean bSet_pm10Value = false;
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
;
} else if(eventType == XmlPullParser.START_TAG) {
String tag_name = xpp.getName();
if (tag_name.equals("resultCode"))
bSet_resultCode = true;
if (tag_name.equals("stationName"))
bSet_stationName = true;
if (tag_name.equals("pm10Value"))
bSet_pm10Value = true;
} else if(eventType == XmlPullParser.TEXT) {
if (bSet_resultCode) {
resultCode = xpp.getText();
tv.append("resultCode: " + resultCode + "\n");
bSet_resultCode = false;
}
if (resultCode.equals("00")) {
if (bSet_stationName) {
stationName = xpp.getText();
tv.append("stationName: " + stationName + "\n");
bSet_stationName = false;
}
if (bSet_pm10Value) {
pm10Value = xpp.getText();
tv.append("pm10Value; " + pm10Value + "\n");
bSet_pm10Value = false;
}
}
} else if(eventType == XmlPullParser.END_TAG) {
;
}
eventType = xpp.next();
}
} catch (Exception e) {
tv.setText(e.getMessage());
}
}
private String downloadUrl(String myurl) throws IOException {
HttpURLConnection conn = null;
try {
URL url = new URL(myurl);
conn = (HttpURLConnection) url.openConnection();
BufferedInputStream buf = new BufferedInputStream(conn.getInputStream());
BufferedReader bufreader = new BufferedReader(new InputStreamReader(buf, "utf-8"));
String line = null;
String page = "";
while((line = bufreader.readLine()) != null) {
page += line;
}
return page;
} finally {
conn.disconnect();
}
}
}
}
반응형
'전공 공부 > 안드로이드프로그래밍' 카테고리의 다른 글
파이어베이스 연동 (0) | 2021.01.13 |
---|---|
공공 데이터 포털의 시도별 실시간 평균정보 조회 API를 이용해 실시간 측정정보 XML을 Parsing 하고 Google Map에 아래와 같이 Marker를 배치해 서울과 부산의 pm10의 값을 출력하는프로그램 (0) | 2021.01.13 |
공공 데이터 포털의 시도별 실시간 측정정보 조회 API를 이용해 실시간 측정정보 XML을Parsing 전에 출력하는 프로그램 (0) | 2021.01.13 |
구글맵 (위성, 일반 지도) (0) | 2021.01.08 |
DB Helper (0) | 2021.01.08 |