本文主要是介绍安卓Kotlin 用GET方法下载数据到ByteArray,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
运行下面的函数即可,传入url
,返回String
@ExperimentalStdlibApi
fun getHttpContent(Url:String, refer: String? = null):ByteArray?{Log.d("Mydl", "getHttp: $Url")var ret:ByteArray? = nullval task = FutureTask(Callable {try {val connection = URL(Url).openConnection() as HttpURLConnectionconnection.requestMethod = "GET"connection.connectTimeout = 8000connection.readTimeout = 8000refer?.let { connection.setRequestProperty("referer", it) }ret = connection.inputStream.readBytes().decodeToString()connection.disconnect()} catch (ex: Exception) {ex.printStackTrace()}return@Callable ret})Thread(task).start()return try {task.get()} catch (ex : Exception){ex.printStackTrace()null}
}
这篇关于安卓Kotlin 用GET方法下载数据到ByteArray的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!