zoukankan      html  css  js  c++  java
  • 036 Android Xutils3网络请求框架使用

    1.xUtils3 介绍

    xUtils 中目前包括了主要的四大模块,分别为 DbUtils 模块、ViewUtils 模块、HttpUtils 模块以及 BitmapUtils 模块。

    xUtils3网络模块大大方便了在实际开发中网络模块的开发,xUtils3网络模块大致包括GET请求、POST请求、如何使用其他请求方式、上传文件、下载文件、使用缓存等功能。

    本文重点讲解:HttpUtils 模块的使用。

    2.xUtils3 的主要特性如下:

    (1)稳定的基石: AbsTask 和统一的回调接口 Callback, 任何异常, 即使你的回调方法实现有异常都会进入 onError,任何情况下 onFinished 总会让你知道任务结束了。

    (2)基于高效稳定的 orm 工具,http 模块得以更方便的实现 cookie (支持 domain、 path、 expiry 等特性)和 缓存(支持 Cache-Control、Last-Modified、 ETag 等特性)的支持。

    (3)有了强大的 http 及其下载缓存的支持,image 模块的实现相当的简洁,并且支持回收被 view 持有,但被 Mem Cache 移除的图片,减少页面回退时的闪烁。

    (4)View 注解模块仅仅400多行代码却灵活的支持了各种 View 注入和事件绑定, 包括拥有多了方法的 listener 的支持。

    3. HttpUtils 模块的主要特性。

    (1)支持同步,异步方式的请求。

    (2)支持大文件上传,上传大文件不会出现 oom 内存溢出情况。

    (3)支持 GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT 的请求。

    (4)下载支持301/302重定向,支持设置是否根据 Content-Disposition 重命名下载的文件。

    (5)返回文本内容的请求(默认只启用了 GET 请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。

    4.xUtils3 使用环境配置

    (1)添加依赖

    implementation 'org.xutils:xutils:3.5.1'

    (2)在manifest文件中添加权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

     (3)对Xutils3进行初始化

    新建一个类MyApplication继承Application:

    package com.example.administrator.test66smartbeijing;
    import android.app.Application;
    import org.xutils.x;
    
    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            x.Ext.init(this);
            x.Ext.setDebug(false); //输出debug日志,开启会影响性能
        }
    }

    在Manifest文件中注册MyApplication

    <application
            android:name=".MyApplication"
            android:allowBackup="true"
            android:icon="@mipmap/icon_150"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

    5.使用案例

    package com.example.administrator.test66smartbeijing.fragment;
    
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import com.example.administrator.test66smartbeijing.R;
    import org.xutils.common.Callback;
    import org.xutils.http.RequestParams;
    import org.xutils.x;
    
    
    /**
     * 利用tabLayout+viewPager实现带顶部菜单栏的fragment
     */
    public class NewsCenterVolleyFragment extends Fragment {
    
        //handler用来处理消息
        @SuppressLint("HandlerLeak")
        Handler handler=new Handler(){
            @SuppressLint("ResourceType")
            @Override
            public void handleMessage(Message msg) {
                //更新ui
                if(msg.what==0x01){
                    //接收到消息后,从当前的fragment跳转到另一个activity中
                    String queryResultStr= (String) msg.obj;
                }
            }
        };
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            getDataFromServer();
            View view=inflater.inflate(R.layout.layout_fm_newscenter_volley,container,false);
            return view;
    
        }
    
        /**
         * 从服务器获取数据
         */
        private void getDataFromServer() {
            //请求地址
            String url="http://118.25.152.62:8080/zhbj/categories.json";
            RequestParams params = new RequestParams(url);
            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    //解析result
                    System.out.println(result);
                }
                //请求异常后的回调方法
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }
                //主动调用取消请求的回调方法
                @Override
                public void onCancelled(CancelledException cex) {
                }
                @Override
                public void onFinished() {
                }
            });
    
        }
    
    
    }

    6.效果展示

    System.out: {"retcode":200,"data":[{"id":10000,"title":"新闻","type":1,"children":[{"id":10007,"title":"北京","type":1,"url":"/10007/list_1.json"},{"id":10006,"title":"中国","type":1,"url":"/10006/list_1.json"},{"id":10008,"title":"国际","type":1,"url":"/10008/list_1.json"},{"id":10010,"title":"体育","type":1,"url":"/10010/list_1.json"},{"id":10091,"title":"生活","type":1,"url":"/10091/list_1.json"},{"id":10012,"title":"旅游","type":1,"url":"/10012/list_1.json"},{"id":10095,"title":"科技","type":1,"url":"/10095/list_1.json"},{"id":10009,"title":"军事","type":1,"url":"/10009/list_1.json"},{"id":10093,"title":"时尚","type":1,"url":"/10093/list_1.json"},{"id":10011,"title":"财经","type":1,"url":"/10011/list_1.json"},{"id":10094,"title":"育儿","type":1,"url":"/10094/list_1.json"},{"id":10105,"title":"汽车","type":1,"url":"/10105/list_1.json"}]},{"id":10002,"title":"专题","type":10,"url":"/10006/list_1.json","url1":"/10007/list1_1.json"},{"id":10003,"title":"组图","type":2,"url":"/10008/list_1.json"},{"id":10004,"title":"互动","type":3,"excurl":"","dayurl":"","weekurl":""}],"extend":[10007,10006,10008,10014,10012,10091,10009,10010,10095]}
  • 相关阅读:
    步入大公司一年及三十而立的一些思考
    let和const
    简明学习webpack
    npm&&yarn和package.json
    基于jQuery.i18n.properties插件实现前端页面国际化
    Sequence Model
    系统性整理项目:第一篇(续篇)-osm存入PostgreSQL+PostGIS并构建路网表结构
    系统性整理项目:第一篇(环境+数据)
    window下源码编译mmcv-full==1.2.1
    多边形平滑算法-ChaikinSmoothing
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10935395.html
Copyright © 2011-2022 走看看