<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
LinearLayout>
class MainActivity : AppCompatActivity() {
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView.settings.javaScriptEnabled = true
webView.webViewClient = WebViewClient()
webView.loadUrl("https://www.baidu.com")
}
}
<uses-permission android:name="android.permission.INTERNET" />
val url = URL("Https://www.baidu.com")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
connection.connectTimeout = 8000
connection.readTimeout = 8000
val input = connection.inputStream
connection.disconnect()
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/sendRequestBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Request" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/respondText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
ScrollView>
LinearLayout>
在这里面我们主要使用了一个新的控件ScrollView,因为由于手机屏幕比较小,有时候过多的内容显示不下,借助ScrollView控件就可以通过滚动屏幕的方式来查看屏幕以外的内容了
另外还有一个按钮和一个TextView,分别用来发送HTTP请球和显示服务器返回的数据的
现在写MainActivity当中的代码来编写具体的逻辑
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sendRequestBtn.setOnClickListener {
sendRequestWithHttpURLConnection()
}
}
private fun sendRequestWithHttpURLConnection() {
//开启线程发起网络请求
thread {
var connection: HttpURLConnection? = null
try {
val response = StringBuilder()
val url = URL("https://www.baidu.com")
connection = url.openConnection() as HttpURLConnection
connection.connectTimeout = 8000
connection.readTimeout = 8000
val input = connection.inputStream
//下面对获取到的输入流进行读取
val reader = BufferedReader(InputStreamReader(input))
reader.use {
reader.forEachLine {
response.append(it)
}
}
showResponse(response.toString())
} catch (e: Exception) {
e.printStackTrace()
} finally {
connection?.disconnect()
}
}
}
private fun showResponse(response: String) {
runOnUiThread {
//在这里进行UI操作,将结果显示到界面上
respondText.text = response
}
}
}
<uses-permissi android:name="android.permission.INTERNET" />
connection.requestMethod = "POST"
val output = DataOutputStream(connection.outputStream)
output.writeBytes("username=admin&password=123456")
在开源盛行的今天,除过HttpURLConnection,也并不是没有其他的选择,有许多出色的网络通信库都可以替代原生的HttpURLConnection,而OkHttp无疑是做的最出色的一个
OkHttp是Square公司开发的一款在接口封装上做的简单易用,就连底层也是自成一派,现在已经成为广大Android开发者首选的网络通信库
OkHttp的项目主页地址是:Http://github.com/square/okhttp
在使用OkHttp之前我们需要现在项目当中添加OkHttp库的依赖
implementation("com.squareup.okhttp3:okhttp:4.10.0")
val client = OkHttpClient()
val request = Request.Builder().build()
val request = Request.Builder()
.url("https://www.baidu.com")
.build()
val response = client.newCall(resquest).execute()
val responseData = response.body?.string()
val requestBody = FormBody.Builder()
.add("username", "admin")
.add("password", "123456")
.build()
val request = Request.Builder()
.url("https://www.baidu.com")
.post(requestBody)
.build()
private fun sendRequestWithOkHttp() {
thread {
try {
val client = OkHttpClient()
val request = Request.Builder()
.url("https://www.baidu.com")
.build()
val response = client.newCall(request).execute()
val responseData = response.body?.string()
if (responseData != null) {
showResponse(responseData)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}