공공데이터(data.go.kr)에서 전주시내버스 앱 개발(Fragment 적용하기)
Android Studio에서 새 프로젝트 추가 후 MyConst.java 에 일반 인증키 번호 추가
1 2 3 |
public class MyConst { public static final String DATA_POST_KEY = "data.go.kr에 받은 인증키 번호"; } |
Traffic 허용해주기 위해 /res/xml/network_security_config.xml 파일 생성
1 2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">openapi.jeonju.go.kr</domain> </domain-config> </network-security-config> |
AndroidManifest.xml 에 위 xml 파일 등록 AdnroidManifest.xml 에 인터넷 사용 가능하도록 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="kr.co.moak.proj001address"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <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/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
/layout/frag_stop.xml 생성
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lstView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |
StopAdapter 클래스 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package kr.co.moak.jeonjubus99; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; public class StopAdapter extends BaseAdapter { Context context; ArrayList<HashMap<String, String>> arList; LayoutInflater mInflater; public StopAdapter(Context context, ArrayList<HashMap<String, String>> arList){ this.context = context; this.arList = arList; mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return arList.size(); } @Override public Object getItem(int i) { return arList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View row = mInflater.inflate(R.layout.stop_list_item, viewGroup, false); final String stopkname = arList.get(i).get("stopkname"); ((TextView) row.findViewById(R.id.stopkname)).setText(stopkname); final String stopx = arList.get(i).get("stopx"); ((TextView) row.findViewById(R.id.stopx)).setText(stopx); final String stopy = arList.get(i).get("stopy"); ((TextView) row.findViewById(R.id.stopy)).setText(stopy); final String stopid = arList.get(i).get("stopid"); ((TextView) row.findViewById(R.id.stopid)).setText(stopid); return row; } } |
StopFragment 클래스 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
package kr.co.moak.jeonjubus99; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class StopFragment extends Fragment { ArrayList<HashMap<String, String>> m_arList; StopAdapter m_adapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.frag_stop, container, false); String apiKey = MyConst.API_KEY; String searchFld = "stopnm"; String searchNm = "전주대"; // 승강장명 목록 조회 String url = "http://openapi.jeonju.go.kr/jeonjubus/openApi/traffic/bus_location2_stopnm_common.do" + "?ServiceKey=" + apiKey + "&searchFld=stopnm&searchNm=" + searchNm; new MyAsyncTask(getContext()).execute(url); ListView lstView = (ListView)view.findViewById(R.id.lstView); m_arList = new ArrayList<>(); m_adapter = new StopAdapter(getContext(), m_arList); lstView.setAdapter(m_adapter); lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(getContext(), m_arList.get(i).get("stopkname"), Toast.LENGTH_SHORT).show(); } }); return view; } class MyAsyncTask extends AsyncTask<String, Integer, Document> { private Context m_context; public MyAsyncTask(Context context){ this.m_context = context; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Document doInBackground(String... urls) { URL url; Document doc = null; try{ url = new URL(urls[0]); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); }catch (Exception e){ Log.v("MYTAG", "Parsing Error"); } Log.v("MYTAG", urls[0]); return doc; } @Override protected void onPostExecute(Document doc) { m_arList.clear(); NodeList nodeList = doc.getElementsByTagName("list"); Log.v("MYTAG", "cnt: " + nodeList.getLength()); for(int i = 0; i<nodeList.getLength(); i++) { HashMap<String, String> hash = new HashMap<>(); NodeList childList = nodeList.item(i).getChildNodes(); for(int j=0; j<childList.getLength(); j++){ hash.put(childList.item(j).getNodeName().toLowerCase(), childList.item(j).getTextContent()); Log.v("MYTAG", childList.item(j).getNodeName().toLowerCase() + " "+ childList.item(j).getTextContent()); } m_arList.add(hash); } m_adapter.notifyDataSetChanged(); } } } |
/layout/activity_main.xml 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <LinearLayout android:id="@+id/linear1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="즐겨찾기" /> <Button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="노선" /> <Button android:id="@+id/b3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="정류장" /> </LinearLayout> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/linear1"> </FrameLayout> </RelativeLayout> |
위 정류장과 유사하게… Continue Reading 공공데이터(data.go.kr)에서 전주시내버스 앱 개발(Fragment 적용하기)