第三方开源库之 Retrofit

当前版本: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'
}
  • 权限
<uses-permission android:name="android.permission.INTERNET"/>
  1. 创建 接收服务器返回数据 的类
    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 +
                    '}';
        }
    }
}

  1. 创建 用于描述网络请求 的接口
/**
 * 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<Translation> getCall();
}

  1. 创建 Retrofit 实例
  2. 创建 网络请求接口实例 并 配置网络请求参数
  3. 发送网络请求(异步 / 同步)
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<Translation> call = request.getCall();

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

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

对比

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

  • Android-Async-Http
  • Volley
  • OkHttp

在这里插入图片描述

发布了226 篇原创文章 · 获赞 66 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/duoduo_11011/article/details/104262474