retrofit2之学习(一)
retrofit :时下流行的网络请求库
使用步骤:
- 添加retrofit库依赖(因为retrofit是基于okhttp存在的,所以也要添加okhttp的依赖)
- 创建接收服务器返回数据的类(我感觉技术一个实体 bean)
- 创建一个用于描述网络请求的接口
- 创建retrofit的实例
- 创建网络请求接口的实例,并配置网络请求参数
- 发送网络请求(同步、异步)
- 处理服务器返回的数据
代码如下:
第一步:添加依赖
- compile 'com.squareup.retrofit2:retrofit:2.0.2'
- compile 'com.squareup.okhttp3:okhttp:3.1.2'
- compile 'com.squareup.retrofit2:converter-gson:2.0.2'
第二步:实体bean
- package com.myjob;
- /**
- * Created by Administrator on 2018/5/10/010.
- * 第一步:接收服务器返回数据的类
- */
- public class Translation {
- /**
- * status : 1
- * content : {"from":"en-EU","to":"zh-CN","out":"示例","vendor":"ciba","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
- * out : 示例
- * vendor : ciba
- * err_no : 0
- */
- private String from;
- private String to;
- private String out;
- private String vendor;
- 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 getOut() {
- return out;
- }
- public void setOut(String out) {
- this.out = out;
- }
- public String getVendor() {
- return vendor;
- }
- public void setVendor(String vendor) {
- this.vendor = vendor;
- }
- public int getErr_no() {
- return err_no;
- }
- public void setErr_no(int err_no) {
- this.err_no = err_no;
- }
- }
- }
第三步:请求接口
- package com.myjob;
- import retrofit2.Call;
- import retrofit2.http.GET;
- /**
- * Created by Administrator on 2018/5/10/010.
- * 第二步:用于创建描述网络请求的接口
- */
- public interface GetRequest_Interface {
- @GET("ajax.php?a=fy&f=auto&t=auto&w=hello%20world")
- Call<Translation> getCall();
- // 注解里传入 网络请求 的部分URL地址
- // Retrofit把网络请求的URL分成了两部分:一部分放在Retrofit对象里,另一部分放在网络请求接口里
- // 如果接口里的url是一个完整的网址,那么放在Retrofit对象里的URL可以忽略
- // getCall()是接受网络请求数据的方法
- }
第四步:发送请求并解析的过程
- package com.myjob;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- /**
- * 第三步:处理返回数据 http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=hello%20world
- */
- public class MainActivity extends AppCompatActivity {
- private Button button;
- private TextView textView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- private void initView() {
- button=findViewById(R.id.btn);
- textView=findViewById(R.id.tv);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- request();//使用retrofit封装的方法
- }
- });
- }
- private void request() {
- //第一步:创建一个retrofit的对象
- final Retrofit retrofit= new Retrofit.Builder()
- .baseUrl("http://fy.iciba.com/")//设置网络请求 的URL
- .addConverterFactory(GsonConverterFactory.create())//设置使用gson解析
- .build();
- //第二步:创建请求接口的实例
- GetRequest_Interface getRequest_interface= retrofit.create(GetRequest_Interface.class);
- //第三步:对发送请求进行封装
- Call<Translation> call=getRequest_interface.getCall();
- //第四步:发送请求
- call.enqueue(new Callback<Translation>() {
- //第五步:请求成功回调
- @Override
- public void onResponse(Call<Translation> call, Response<Translation> response) {
- //处理返回的数据结果
- // response.body().show();结果可在控制台查看 一种方法
- //第二种方法 解析了部分
- textView.setText("获取的到信息是:状态为"+response.body().getStatus()+"内容为:"+
- response.body().getContent().getFrom()+","+response.body().getContent().getOut() + "等等");
- }
- @Override//请求失败时回调
- public void onFailure(Call<Translation> call, Throwable t) {
- //
- System.out.println("请求失败");
- }
- });
- }
- }
第五步:简单的效果图
-----------------------------------------------------------------------------------------------------------------------------
参考文献:https://blog.csdn.net/carson_ho/article/details/73732076
项目源码:https://download.csdn.net/download/qq_36636969/10405724