企业网站怎么做才能留住客户,app定制软件开发哪家好,网站编排页面,建筑网络工程教程WebView 已经在后台帮我们处理好了发送HTTP请求、接收服务器响应、解析返回数据#xff0c;以及最终的页面展示这几步工作。只不过它封装得实在是太好了#xff0c;反而使得我们不能那么直观地看出HTTP到底是如何工作的。因此#xff0c;接下来我们通过手动发送HTTP请求的方…WebView 已经在后台帮我们处理好了发送HTTP请求、接收服务器响应、解析返回数据以及最终的页面展示这几步工作。只不过它封装得实在是太好了反而使得我们不能那么直观地看出HTTP到底是如何工作的。因此接下来我们通过手动发送HTTP请求的方式更加深入地理解这个过程。 在过去Android上发送HTTP请求一般有两种方式HttpURLConnection 和HttpClient 。不过由于HttpClient 存在API数量过多、扩展困难等缺点Android团队越来越不建议我们使用这种方式。终于在Android 6.0 系统中HttpClient 的功能被完全移除了标志着此功能被正式弃用。 现在官方建议使用的HttpURLConnection。
HttpURLConnection
class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) sendRequestBtn.setOnClickListener {sendRequestWithHttpURLConnection() }}/**在这个方法中先是开启了一个子线程然后在子线程里使用HttpURLConnection 发出一条HTTP请求。*/private fun sendRequestWithHttpURLConnection() { // 开启线程发起网络请求thread {var connection: HttpURLConnection? null try {val response StringBuilder()//首先需要获取HttpURLConnection 的实例一般只需创建一个URL对象并传入目标的网络地 址val url URL(https://www.baidu.com)//然后调用一下openConnection()方法即可connection url.openConnection() as HttpURLConnection //设置HTTP请求所使用的方法、连接超时、读取超时的毫秒数connection.requestMethod GETconnection.connectTimeout 8000 connection.readTimeout 8000//之后再调用getInputStream()方法就可以获取到服务器返回的输入流了val input connection.inputStream//接着利用BufferedReader 对服务器返回的流进行读取val reader BufferedReader(InputStreamReader(input)) reader.use {reader.forEachLine {response.append(it) }}//将结果传入showResponse() 方法中。showResponse(response.toString()) } catch (e: Exception) {e.printStackTrace() } finally {connection?.disconnect() }} }private fun showResponse(response: String) { runOnUiThread {// 在这里进行UI操作将结果显示到界面上responseText.text response }}
}!-- AndroidManifest.xml 声明一下网络权限--
manifest xmlns:androidhttp://schemas.android.com/apk/res/android packagecom.example.networktestuses-permission android:nameandroid.permission.INTERNET /...
/manifest!-- activity_main.xml --
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:orientationverticalandroid:layout_widthmatch_parent android:layout_heightmatch_parent Buttonandroid:idid/sendRequestBtn android:layout_widthmatch_parent android:layout_heightwrap_content android:textSend Request /ScrollView android:layout_widthmatch_parent android:layout_heightmatch_parent TextViewandroid:idid/responseText android:layout_widthmatch_parent android:layout_heightwrap_content //ScrollView/LinearLayout提交数据给服务器
connection.requestMethod POST
val output DataOutputStream(connection.outputStream) output.writeBytes(usernameadminpassword123456)OkHttp
dependencies {...implementation com.squareup.okhttp3:okhttp:4.1.0
}下面我们来看一下OkHttp 的具体用法。 首先需要创建一个OkHttpClient的实例如下所示:
val client OkHttpClient()接下来如果想要发起一条HTTP请求就需要创建一个Request对象:
val request Request.Builder().build()当然上述代码只是创建了一个空的Request对象并没有什么实际作用我们可以在最终的 build()方法之前连缀很多其他方法来丰富这个Request对象。比如可以通过url()方法来设置目标的网络地址如下所示:
val request Request.Builder().url(https://www.baidu.com).build()之后调用OkHttpClient 的newCall()方法来创建一个Call对象并调用它的execute()方法 来发送请求并获取服务器返回的数据写法如下:
val response client.newCall(request).execute()Response对象就是服务器返回的数据了我们可以使用如下写法来得到返回的具体内容:
val responseData response.body?.string()如果是发起一条POST请求会比GET请求稍微复杂一点我们需要先构建一个Request Body 对象来存放待提交的参数如下所示:
val requestBody FormBody.Builder() .add(username, admin).add(password, 123456) .build()然后在Request.Builder 中调用一下post()方法并将RequestBody对象传入:
val request Request.Builder() .url(https://www.baidu.com).post(requestBody) .build()接下来的操作就和GET请求一样了调用execute()方法来发送请求并获取服务器返回的数据即可。