API 데이터 형태
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity
val serverAddress = "API 주소"
thread {
val url = URL(serverAddress)
val httpURLConnection = url.openConnection() as HttpURLConnection
val inputStreamReader = InputStreamReader(httpURLConnection.inputStream, "UTF-8")
val bufferedReader = BufferedReader(inputStreamReader)
var str:String? = null
val stringBuffer = StringBuffer()
do{
str = bufferedReader.readLine()
if(str != null){
stringBuffer.append(str)
}
}while(str != null)
val data = stringBuffer.toString()
runOnUiThread{
textView.text = ""
}
}
- JSON 데이터 분석
- { } : JSONObject
- [ ] : JSONArray
thread {
val root = JSONObject(data)
val boardsArray = root.getJSONArray("boards")
for(idx in 0 until boardsArray.length()) {
val boardsObject = boardsArray.getJSONObject(idx)
val board = boardsObject.getString("board")
val title = boardsObject.getString("title")
val pages = boardsObject.getInt("pages")
val image_limit = boardsObject.getInt("image_limit")
val cooldowns = boardsObject.getJSONObject("cooldowns")
val threads = cooldowns.getInt("threads")
val replies = cooldowns.getInt("replies")
val images = cooldowns.getInt("images")
runOnUiThread {
textViewData.append("board : $board\n")
textViewData.append("title : $title\n")
textViewData.append("pages : $pages\n")
textViewData.append("image_limit : $image_limit\n")
textViewData.append("[cooldowns]\n")
textViewData.append("threads : $threads\n")
textViewData.append("replies : $replies\n")
textViewData.append("images : $images\n\n")
}
}
}
활용 API (Free and Open Public APIs)
API를 활용한 데이터 처리에 대한 내용이 상세하게 잘 설명되어 있어, 많은 도움이 되었습니다. 특히 JSON 데이터 분석 부분을 이해하는데 크게 도움이 되었네요. 좋은 정보 공유 감사드립니다!