Adjust the receiving protocol (the use of https protocol can be ignored)
Because I simply built a web server with apache, which only supports the http protocol, so the following operations should be performed to ensure that the data can be received.
- Created a new configuration file network_config.xml under the xml folder of res
<?xml version="1.0" encoding="utf-8" ?> <network-security-config> <base-config cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system"/> </trust-anchors> </base-config> </network-security-config>
- Make the following changes in the AndroidManifest.xml configuration file (bold)
<application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.OkHttp" android:networkSecurityConfig="@xml/network_config" tools:targetApi="31">
Write the layout file
Only one button and one textview are defined here.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="open" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
access network
Here I choose to use OkHttp to access the network, write a method, and call this method when the button is clicked.
fun HttpRquest() {
thread {
try {
val okhttpclient = OkHttpClient()
val request = okhttp3.Request.Builder()
.url("http://43.137.44.141/get_data.xml")
.build()
val initcontent = okhttpclient.newCall(request).execute()
val content = initcontent.body?.string()
if (content != null) {
//调用解析方法,下文中写了。
parseXML(content)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Analytical data
Data format display:
//要解析的数据格式
<apps>
<app>
<id>1</id>
<name>Google Maps</name>
<version>1.0</version>
</app>
<app>
<id>2</id>
<name>Chome</name>
<version>2.1</version>
</app>
<app>
<id>3</id>
<name>Google Play</name>
<version>2.3</version>
</app>
</apps>
A method for parsing data is defined, and the parsed data is filled into the textview.
fun parseXML(Data: String) {
runOnUiThread {
val content=StringBuilder()
try {
val factory = XmlPullParserFactory.newInstance()
val pullParser = factory.newPullParser()
pullParser.setInput(StringReader(Data))
var eventType = pullParser.eventType
var id = ""
var name = ""
var version = ""
while (eventType != XmlPullParser.END_DOCUMENT){
val node=pullParser.name
when(eventType){
XmlPullParser.START_TAG->{
when(node){
"id"->id=pullParser.nextText()
"name"->name=pullParser.nextText()
"version"->version=pullParser.nextText()
}
}
XmlPullParser.END_TAG->{
if ("app"==node){
content.append("id:$id\tname:$name\tversion:$version\n")
Log.d("information","id:$id\tname:$name\tversion:$version")
}
}
}
eventType=pullParser.next()
}
binding.tv.text=content.toString()
}catch (e:Exception){
e.printStackTrace()
}
}
}
Attach the total code for easy copying
package com.example.okhttp
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.okhttp.databinding.ActivityMainBinding
import okhttp3.OkHttpClient
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.io.StringReader
import kotlin.concurrent.thread
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.button.setOnClickListener {
HttpRquest()
}
}
fun HttpRquest() {
thread {
try {
val okhttpclient = OkHttpClient()
val request = okhttp3.Request.Builder()
.url("http://43.137.44.141/get_data.xml")
.build()
val initcontent = okhttpclient.newCall(request).execute()
val content = initcontent.body?.string()
if (content != null) {
parseXML(content)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
fun parseXML(Data: String) {
runOnUiThread {
val content=StringBuilder()
try {
val factory = XmlPullParserFactory.newInstance()
val pullParser = factory.newPullParser()
pullParser.setInput(StringReader(Data))
var eventType = pullParser.eventType
var id = ""
var name = ""
var version = ""
while (eventType != XmlPullParser.END_DOCUMENT){
val node=pullParser.name
when(eventType){
XmlPullParser.START_TAG->{
when(node){
"id"->id=pullParser.nextText()
"name"->name=pullParser.nextText()
"version"->version=pullParser.nextText()
}
}
XmlPullParser.END_TAG->{
if ("app"==node){
content.append("id:$id\tname:$name\tversion:$version\n")
Log.d("information","id:$id\tname:$name\tversion:$version")
}
}
}
eventType=pullParser.next()
}
binding.tv.text=content.toString()
}catch (e:Exception){
e.printStackTrace()
}
}
}
}
attached renderings