Retrofit of third party open source libraries

Current Version: 2.7.1
official document: HTTPS: //square.github.io/retrofit/
Retrofit is a RESTful HTTP network encapsulation frames request.

Brief introduction

Here Insert Picture Description

use

  1. Add rely Retrofit library
  • rely
dependencies {
    ...
    implementation 'com.squareup.retrofit2:retrofit:2.7.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
}
  • Competence
<uses-permission android:name="android.permission.INTERNET"/>
  1. Receiving server returns the data to create a class
    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. Creating a requested network interface description
/**
 * 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. Creating Retrofit examples
  2. Examples of creating a network request interface and configure the network parameters request
  3. A network request (asynchronous / synchronous)
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, "连接失败");
            }
        });
    }
}

Compared

In addition to Retrofit, now in the mainstream of the Android framework network requests:

  • Android-Async-Http
  • Volley
  • OkHttp

Here Insert Picture Description

Published 226 original articles · won praise 66 · views 210 000 +

Guess you like

Origin blog.csdn.net/duoduo_11011/article/details/104262474
Recommended