노선버스 위치 조회 Bus Position 프로젝트
문제
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.ac.skuniv.a1210practice_5">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.1210practice_5"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.buspositions.MainActivity">
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</ScrollView>
MainActivity.java
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);
tv = (TextView) findViewById(R.id.data);
// 버스위치 추출을 위한 공공 DB API의 URL
String serviceUrl = "http://ws.bus.go.kr/api/rest/buspos/getBusPosByRtid";
// 버스위치 추출을 위한 공공 DB API 키
String serviceKey = "U6ZN%2BCI1KQOG1WtSS%2B1OyayCLzUnBl01eZa1oU9xmHDudmO%2BdaQTSudLTVDm0vuAqso9jxNK9Gi%2BBGH82rTC6w%3D%3D";
// 노선ID "1164"의 버스 경로 ID
String busRouteId = "100100171"; // 1164버스에 해당하는 busRouteID
// 공공 DB API 호출을 위한 URL
String strUrl = serviceUrl+"?ServiceKey="+serviceKey+"&busRouteId="+busRouteId;
// URL에 해당하는 문서 추출을 위한 객체 생성
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) {
String headerCd = "";
String gpsX = "";
String gpsY = "";
String plainNo = "";
boolean bSet_headerCd = false;
boolean bSet_gpsX = false;
boolean bSet_gpsY = false;
boolean bSet_plainNo = false;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(result));
int eventType = xpp.getEventType();
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("headerCd"))
bSet_headerCd = true;
if (tag_name.equals("gpsX")) // 시작 태그가 gpsX인 경우(버스위치의 위도)
bSet_gpsX = true;
if (tag_name.equals("gpsY")) // 시작 태그가 gpsY인 경우(버스위치의 위도)
bSet_gpsY = true;
if (tag_name.equals("plainNo")) // 시작 태그가 plainNo 인 경우(버스번호)
bSet_plainNo = true;
} else if(eventType == XmlPullParser.TEXT) {
if (bSet_headerCd) {
headerCd = xpp.getText();
tv.append("headerCd: " + headerCd + "\n");
bSet_headerCd = false;
}
if (headerCd.equals("0")) {
// 시작태그가 <gpsY>인 경우(버스위치의 경도)
if (bSet_gpsX) {
gpsX = xpp.getText(); // <gpsX>와 </gpsX>사이에 있는 데이터 추출
tv.append("gpsX: " + gpsX + "\n"); // 버스위치의 위도 출력
bSet_gpsX = false;
}
// 시작태그가 <gpsX>인 경우(버스위치의 위도)
if (bSet_gpsY) {
gpsY = xpp.getText();
tv.append("gpsY: " + gpsY + "\n");
bSet_gpsY = false;
}
if (bSet_plainNo) {
plainNo = xpp.getText();
tv.append("plainNo; " + plainNo + "\n");
bSet_plainNo = 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();
}
}
}
}