2024年了现在安卓可以用的三方网络框架汇总

13 篇文章 0 订阅
7 篇文章 0 订阅

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")
                }

            })
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南一舟110

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值