您当前的位置: 首页 >  网络

Kevin-Dev

暂无认证

  • 3浏览

    0关注

    544博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android -- 网络请求】Retrofit 的基本使用

Kevin-Dev 发布时间:2020-02-11 14:19:13 ,浏览量:3

在这里插入图片描述

当前版本:2.7.1 官方文档:https://square.github.io/retrofit/ Retrofit 是一个 RESTful 的 HTTP 网络请求框架的封装。

一、简介

在这里插入图片描述

二、使用

1. 添加Retrofit库的依赖

  • 依赖
dependencies {
    ...
    implementation 'com.squareup.retrofit2:retrofit:2.7.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
}
  • 权限

2. 创建 接收服务器返回数据 的类 Translation.java

/**
 * created on 2020/2/11 10:18
 *
 * @author Scarf Gong
 */
public class Translation {

    /**
     * status : 1
     * content : {"from":"en-EU","to":"zh-CN","vendor":"wps","out":"你好世界","ciba_use":"来自机器翻译。","ciba_out":"","err_no":0}
     */

    private int status;
    private ContentBean content;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public ContentBean getContent() {
        return content;
    }

    public void setContent(ContentBean content) {
        this.content = content;
    }

    public static class ContentBean {
        /**
         * from : en-EU
         * to : zh-CN
         * vendor : wps
         * out : 你好世界
         * ciba_use : 来自机器翻译。
         * ciba_out :
         * err_no : 0
         */

        private String from;
        private String to;
        private String vendor;
        private String out;
        private String ciba_use;
        private String ciba_out;
        private int err_no;

        public String getFrom() {
            return from;
        }

        public void setFrom(String from) {
            this.from = from;
        }

        public String getTo() {
            return to;
        }

        public void setTo(String to) {
            this.to = to;
        }

        public String getVendor() {
            return vendor;
        }

        public void setVendor(String vendor) {
            this.vendor = vendor;
        }

        public String getOut() {
            return out;
        }

        public void setOut(String out) {
            this.out = out;
        }

        public String getCiba_use() {
            return ciba_use;
        }

        public void setCiba_use(String ciba_use) {
            this.ciba_use = ciba_use;
        }

        public String getCiba_out() {
            return ciba_out;
        }

        public void setCiba_out(String ciba_out) {
            this.ciba_out = ciba_out;
        }

        public int getErr_no() {
            return err_no;
        }

        public void setErr_no(int err_no) {
            this.err_no = err_no;
        }

        @Override
        public String toString() {
            return "ContentBean{" +
                    "from='" + from + '\'' +
                    ", to='" + to + '\'' +
                    ", vendor='" + vendor + '\'' +
                    ", out='" + out + '\'' +
                    ", ciba_use='" + ciba_use + '\'' +
                    ", ciba_out='" + ciba_out + '\'' +
                    ", err_no=" + err_no +
                    '}';
        }
    }
}

3. 创建 用于描述网络请求 的接口

/**
 * created on 2020/2/11 10:22
 *
 * @author Scarf Gong
 */
public interface IRequest {
    @GET("ajax.php?a=fy&f=auto&t=auto&w=hello%20world")
    Call getCall();
}

4. 创建 Retrofit 实例 5. 创建 网络请求接口实例 并 配置网络请求参数 6. 发送网络请求(异步 / 同步)

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        request();
    }

    private void request() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://fy.iciba.com/") // 设置 网络请求 Url
                .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                .build();

        IRequest request = retrofit.create(IRequest.class);

        //对发送请求进行封装
        Call call = request.getCall();

        call.enqueue(new Callback() {
            //请求成功时回调
            @Override
            public void onResponse(Call call, Response response) {
                //处理返回的数据结果
                int status = response.body().getStatus();
                Log.d(TAG, "onResponse: " + status);
            }

            //请求失败时回调
            @Override
            public void onFailure(Call call, Throwable throwable) {
                Log.d(TAG, "连接失败");
            }
        });
    }
}
对比

除了Retrofit,如今Android中主流的网络请求框架有:

  • Android-Async-Http
  • Volley
  • OkHttp

在这里插入图片描述

关注
打赏
1658837700
查看更多评论
立即登录/注册

微信扫码登录

0.0666s