2024 还可以用的网络框架 xutils3 vollery okhttp
implementation("com.android.volley:volley:1.2.1")
val testUrl = "https://wanandroid.com/harmony/index/json"
inflate.btn.setOnClickListener {
newRequestQueue.add(object : StringRequest(testUrl, object : Response.Listener<String> {
override fun onResponse(p0: String?) {
println(p0)
}
}, object : Response.ErrorListener {
override fun onErrorResponse(p0: VolleyError?) {
println("error" + p0?.message)
}
}) {
})
}
val imload = ImageLoader(Volley.newRequestQueue(this@TestPic), object : ImageCache {
override fun getBitmap(p0: String?): Bitmap? {
return null;
}
override fun putBitmap(p0: String?, p1: Bitmap?) {
}
})
inflate.nm.setImageUrl(
"https://search-operate.cdn.bcebos.com/e8cbce1d53432a6950071bf26b640e2b.gif",
imload
)
xutils3
implementation 'org.xutils:xutils:3.8.12'
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
package com.example.myapplication
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.databinding.TestXutilsBinding
import org.xutils.common.Callback
import org.xutils.common.task.PriorityExecutor
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.File
class XlutilsDemo : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val inflate = TestXutilsBinding.inflate(layoutInflater)
setContentView(inflate.root)
inflate.btnGet.setOnClickListener {
val requestParses = RequestParams("https://www.wanandroid.com/article/list/0/json");
requestParses.addBodyParameter("a", 123);
x.http().get(requestParses, object : Callback.CommonCallback<String> {
override fun onSuccess(result: String?) {
println("onSuccess${result}")
}
override fun onError(ex: Throwable?, isOnCallback: Boolean) {
println("onError${ex?.message}")
}
override fun onCancelled(cex: Callback.CancelledException?) {
println("onCancelled")
}
override fun onFinished() {
println("onFinished")
}
})
}
inflate.btnPost.setOnClickListener {
val requestParses = RequestParams("https://www.wanandroid.com/user/login");
requestParses.addBodyParameter("username", "gsc");
requestParses.addBodyParameter("password", 123456);
x.http().post(requestParses, object : Callback.CommonCallback<String> {
override fun onSuccess(result: String?) {
println("onSuccess${result}")
}
override fun onError(ex: Throwable?, isOnCallback: Boolean) {
println("onError${ex?.message}")
}
override fun onCancelled(cex: Callback.CancelledException?) {
println("onCancelled")
}
override fun onFinished() {
println("onFinished")
}
})
}
inflate.btnDownload.setOnClickListener {
val requestParses =
RequestParams("https://www.baidu.com/img/flexible/logo/pc/result.png");
requestParses.isAutoResume = true
requestParses.isAutoRename = false
requestParses.executor = PriorityExecutor(2, true)
requestParses.isCancelFast = true
requestParses.saveFilePath = "/sdcard/a.png"
x.http().get(requestParses, object : Callback.CommonCallback<File> {
override fun onSuccess(result: File?) {
println("下载onSuccess${result?.absoluteFile?.name}")
}
override fun onError(ex: Throwable?, isOnCallback: Boolean) {
println("下载onError${ex?.message}")
}
override fun onCancelled(cex: Callback.CancelledException?) {
println("下载onCancelled")
}
override fun onFinished() {
println("下载onFinished")
}
})
}
}
}