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">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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"
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;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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) {
tv.setText(result);
}
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();
}
}
}
}
반응형
'전공 공부 > 안드로이드프로그래밍' 카테고리의 다른 글
공공 데이터 포털의 시도별 실시간 평균정보 조회 API를 이용해 실시간 측정정보 XML을 Parsing 하고 Google Map에 아래와 같이 Marker를 배치해 서울과 부산의 pm10의 값을 출력하는프로그램 (0) | 2021.01.13 |
---|---|
공공 데이터 포털의 시도별 실시간 측정정보 조회 API를 이용해 실시간측정정보 XML을 Parsing 하여 아래와 같이 <stationName>과 <pm10Value>를 출력하는 프로그램 (0) | 2021.01.13 |
구글맵 (위성, 일반 지도) (0) | 2021.01.08 |
DB Helper (0) | 2021.01.08 |
2개의 액티비티 (계산기 EditText, RadioButton, Button) (0) | 2021.01.08 |