<Android> 우체국 오픈API를 활용한 우편번호 조회 예제
우체국 오픈API를 이용하여 우편번호를 얻어오는 안드로이드 예제입니다.
먼저 우체국 오픈API를 사용하기 위해 아래 사이트에서 인증키를 발급받읍시다!
http://biz.epost.go.kr/customCenter/custom/custom_9.jsp?subGubun=sub_3&subGubun_1=cum_17&gubun=m07
다음으로 안드로이드 프로젝트를 생성합니다.
이 때, AndroidManifest.xml 에 다음과 같이 권한을 설정해둡시다! (인터넷을 사용해야 하니까..!)
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
그 다음 주소를 사용자로부터 입력받고
우체국 오픈API로부터 받아온 우편주소를 출력하는 액티비티는 다음과 같이 구성하였습니다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}">
<TextView
android:id="@+id/textPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="주소입력 예)정릉동" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/addressedit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10">
<requestFocus />
</EditText>
<Button
android:id="@+id/btnsearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="검색" />
</LinearLayout>
<ListView
android:id="@+id/addresslist"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
마지막으로 메인액티비티 코드입니다.
우체국 오픈API로부터 받아온 우편주소는 XML형태이기 때문에
Dom을 사용하여 쉽게 정보를 뽑아내고 있습니다.
안드로이드는 기본적으로 네트워크 통신은 스레드에서만 하기를 권장하므로
AsyncTask를 사용하여 구현하였습니다.
MainActivity.java
package worldpay.com.post;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class MainActivity extends Activity {
//우체국 오픈api인증키
private String key = "우체국 API키";
private TextView addressEdit;
private Button searchBtn;
private ListView addressListView;
private ArrayAdapter<String> addressListAdapter;
//사용자가 입력한 주소
private String putAddress;
//우체국으로부터 반환 받은 우편주소 리스트
private ArrayList<String> addressSearchResultArr = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addressEdit = (EditText) findViewById(R.id.addressedit);
searchBtn = (Button) findViewById(R.id.btnsearch);
addressListView = (ListView) findViewById(R.id.addresslist);
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAddress(addressEdit.getText().toString());
}
});
}
private void getAddress(String kAddress) {
putAddress = kAddress;
new GetAddressDataTask().execute();
}
private class GetAddressDataTask extends AsyncTask<String, Void, HttpResponse> {
@Override
protected HttpResponse doInBackground(String... urls) {
HttpResponse response = null;
final String apiurl = "http://biz.epost.go.kr/KpostPortal/openapi";
ArrayList<String> addressInfo = new ArrayList<String>();
HttpURLConnection conn = null;
try {
StringBuffer sb = new StringBuffer(3);
sb.append(apiurl);
sb.append("?regkey=" + key + "&target=post&query=");
sb.append(URLEncoder.encode(putAddress, "EUC-KR"));
String query = sb.toString();
URL url = new URL(query);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("accept-language", "ko");
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
byte[] bytes = new byte[4096];
InputStream in = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) {
int red = in.read(bytes);
if (red < 0) break;
baos.write(bytes, 0, red);
}
String xmlData = baos.toString("utf-8");
baos.close();
in.close();
conn.disconnect();
Document doc = docBuilder.parse(new InputSource(new StringReader(xmlData)));
Element el = (Element) doc.getElementsByTagName("itemlist").item(0);
for (int i = 0; i < ((Node) el).getChildNodes().getLength(); i++) {
Node node = ((Node) el).getChildNodes().item(i);
if (!node.getNodeName().equals("item")) {
continue;
}
String address = node.getChildNodes().item(1).getFirstChild().getNodeValue();
String post = node.getChildNodes().item(3).getFirstChild().getNodeValue();
Log.w("jaeha", "address = " + address);
addressInfo.add(address + "\n우편번호:" + post.substring(0, 3) + "-" + post.substring(3));
}
addressSearchResultArr = addressInfo;
publishProgress();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) conn.disconnect();
} catch (Exception e) {
}
}
return response;
}
@Override
protected void onProgressUpdate(Void... values) {
//TODO Auto-generated method stub
super.onProgressUpdate(values);
String[] addressStrArray = new String[addressSearchResultArr.size()];
addressStrArray = addressSearchResultArr.toArray(addressStrArray);
addressListAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, addressStrArray);
addressListView.setAdapter(addressListAdapter);
}
}
}