[Android development] POST request

POST request

class MainActivity : AppCompatActivity() {
    
    
    private var accEdt: EditText? = null
    private var pwdEdt: EditText? = null

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        accEdt = findViewById(R.id.account)
        pwdEdt = findViewById(R.id.pwd)
    }

    fun myclick(view: View) {
    
    
        when (view.id) {
    
    
            R.id.get -> object : Thread() {
    
    
                override fun run() {
    
    
                    super.run()
                    get()
                }
            }.start()

            R.id.post -> {
    
    
                val account = accEdt!!.text.toString()
                val pwd = pwdEdt!!.text.toString()
                object : Thread() {
    
    
                    override fun run() {
    
    
                        super.run()
                        post(account, pwd)
                    }
                }.start()
            }
        }
    }

    fun post(account: String, pwd: String) {
    
    
        try {
    
    
            //HttpURLConnection
            //1.实例化一个URL对象
            val url = URL("http://www.imooc.com/api/okhttp/postmethod")

            //2.获取HttpURLConnection实例
            val conn = url.openConnection() as HttpURLConnection

            //3.设置和请求相关的属性
            //请求方式
            conn.requestMethod = "POST"
            //设置允许输出
            conn.doOutput = true
            //请求超时时长
            conn.connectTimeout = 6000
            //设置请求数据的类型
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
            //获取输出流(请求正文)
            val out = conn.outputStream
            //写数据
            out.write("account=$account&pwd=$pwd".toByteArray())

            //4.获取响应码
            if (conn.responseCode == HttpURLConnection.HTTP_OK) {
    
    
                //5.判断响应码并获取响应数据
                //获取响应的流
                val inputStream = conn.inputStream
                val b = ByteArray(1024)
                var len = 0
                val baos = ByteArrayOutputStream()
                //在循环中读取输入的流
                //in.read(b); //该方法返回值是int类型数据,代表的是实际读到的数据长度
                while (inputStream.read(b).also {
    
     len = it } > -1) {
    
    
                    //将字节数组里面的内容存/写入缓存流
                    //参数1:待写入的数组
                    //参数2:起点
                    //参数3:长度
                    baos.write(b, 0, len)
                }
                val s = String(baos.toByteArray())
                Log.e("TAG", "$s====")
            }
        } catch (e: IOException) {
    
    
            e.printStackTrace()
        }
    }

GET & POST comparison

GET
  • Requests can be cached, kept in browser history, and bookmarked
  • Not suitable for processing sensitive data
  • There are restrictions on the data length
  • Used to obtain data
POST
  • Requests cannot be cached, cannot be kept in browser history, cannot be bookmarked
  • safer
  • No limitation on data length
  • Used to submit data

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/113092497