반응형
Http Network
설명
- 인터넷 사용시 퍼미션 지정 필요
코드
manifests/AndroidManifest.xml<uses-permission android:name="android.permission.INTERNET" />
MainActivity
class MainActivity : AppCompatActivity() {
private var textView: TextView? = null
private var button: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button?.setOnClickListener {
NetworkThread().start()
}
}
inner class NetworkThread : Thread() {
override fun run() {
val site = "http://www.google.com"
val url = URL(site)
val connection = url.openConnection()
val input = connection.getInputStream()
val isr = InputStreamReader(input, "UTF_8")
val br = BufferedReader(isr)
val text = StringBuilder()
while (true) {
val line = br.readLine()
if (line == null) {
break
} else {
text.append(line)
}
}
runOnUiThread {
textView?.text = text.toString()
}
}
}
}
참고
XML Handling
코드
MainActivityclass MainActivity : AppCompatActivity() {
private var textView: TextView? = null
private var button: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button?.setOnClickListener {
val xml = """
<data>
<item>
<name>john</name>
<age>23</age>
<weight>56.6</weight>
</item>
<item>
<name>tom</name>
<age>26</age>
<weight>66.6</weight>
</item>
</data>
""".trimIndent()
val factory = DocumentBuilderFactory.newInstance()
val builder = factory.newDocumentBuilder()
val doc = builder.parse(InputSource(StringReader(xml)))
val root = doc.documentElement
val itemNodeList = root.getElementsByTagName("item")
textView?.text = ""
for (i in 0 until itemNodeList.length) {
val itemNode = itemNodeList.item(i) as Element
val nameNode = itemNode.getElementsByTagName("name").item(0) as Element
val ageNode = itemNode.getElementsByTagName("age").item(0) as Element
val weightNode = itemNode.getElementsByTagName("weight").item(0) as Element
textView?.append("name : ${nameNode.textContent}\n")
textView?.append("age : ${ageNode.textContent}\n")
textView?.append("weight : ${weightNode.textContent}\n\n")
}
}
}
}
참고
Json Handling
코드
MainActivityclass MainActivity : AppCompatActivity() {
private var textView: TextView? = null
private var button: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button?.setOnClickListener {
val json = """
[
{
"name": "john",
"age": 23,
"weight": 56.6
},
{
"name": "tom",
"age": 26,
"weight": 66.6
}
]
""".trimIndent()
val root = JSONArray(json)
textView?.text = ""
for (i in 0 until root.length()) {
val obj = root.getJSONObject(i)
val name = obj.getString("name")
val age = obj.getString("age")
val weight = obj.getString("weight")
textView?.append("name : ${name}\n")
textView?.append("age : ${age}\n")
textView?.append("weight : ${weight}\n\n")
}
}
}
}
참고
반응형
'Development > Android' 카테고리의 다른 글
[Android] Retrofit (0) | 2021.02.09 |
---|---|
[Android] Lottie (0) | 2021.02.09 |
[Android] File IO (0) | 2021.02.09 |
[Android] Permission (0) | 2021.02.09 |
[Android] Notification (0) | 2021.02.09 |