retrofit2之学习(一)

retrofit :时下流行的网络请求库

使用步骤:

  1. 添加retrofit库依赖(因为retrofit是基于okhttp存在的,所以也要添加okhttp的依赖)
  2. 创建接收服务器返回数据的类(我感觉技术一个实体 bean)
  3. 创建一个用于描述网络请求的接口
  4. 创建retrofit的实例
  5. 创建网络请求接口的实例,并配置网络请求参数
  6. 发送网络请求(同步、异步)
  7. 处理服务器返回的数据   

代码如下:

第一步:添加依赖

[html]  view plain  copy
  1. compile 'com.squareup.retrofit2:retrofit:2.0.2'  
  2.     compile 'com.squareup.okhttp3:okhttp:3.1.2'  
  3.     compile 'com.squareup.retrofit2:converter-gson:2.0.2'  

第二步:实体bean

[html]  view plain  copy
  1. package com.myjob;  
  2.   
  3. /**  
  4.  * Created by Administrator on 2018/5/10/010.  
  5.  * 第一步:接收服务器返回数据的类  
  6.  */  
  7.   
  8. public class Translation {  
  9.     /**  
  10.      * status : 1  
  11.      * content : {"from":"en-EU","to":"zh-CN","out":"示例","vendor":"ciba","err_no":0}  
  12.      */  
  13.   
  14.     private int status;  
  15.     private ContentBean content;  
  16.   
  17.     public int getStatus() {  
  18.         return status;  
  19.     }  
  20.   
  21.     public void setStatus(int status) {  
  22.         this.status = status;  
  23.     }  
  24.   
  25.     public ContentBean getContent() {  
  26.         return content;  
  27.     }  
  28.   
  29.     public void setContent(ContentBean content) {  
  30.         this.content = content;  
  31.     }  
  32.   
  33.     public static class ContentBean {  
  34.         /**  
  35.          * from : en-EU  
  36.          * to : zh-CN  
  37.          * out : 示例  
  38.          * vendor : ciba  
  39.          * err_no : 0  
  40.          */  
  41.   
  42.         private String from;  
  43.         private String to;  
  44.         private String out;  
  45.         private String vendor;  
  46.         private int err_no;  
  47.   
  48.         public String getFrom() {  
  49.             return from;  
  50.         }  
  51.   
  52.         public void setFrom(String from) {  
  53.             this.from = from;  
  54.         }  
  55.   
  56.         public String getTo() {  
  57.             return to;  
  58.         }  
  59.   
  60.         public void setTo(String to) {  
  61.             this.to = to;  
  62.         }  
  63.   
  64.         public String getOut() {  
  65.             return out;  
  66.         }  
  67.   
  68.         public void setOut(String out) {  
  69.             this.out = out;  
  70.         }  
  71.   
  72.         public String getVendor() {  
  73.             return vendor;  
  74.         }  
  75.   
  76.         public void setVendor(String vendor) {  
  77.             this.vendor = vendor;  
  78.         }  
  79.   
  80.         public int getErr_no() {  
  81.             return err_no;  
  82.         }  
  83.   
  84.         public void setErr_no(int err_no) {  
  85.             this.err_no = err_no;  
  86.         }  
  87.     }  
  88.   
  89. }  

第三步:请求接口

[html]  view plain  copy
  1. package com.myjob;  
  2.   
  3. import retrofit2.Call;  
  4. import retrofit2.http.GET;  
  5.   
  6. /**  
  7.  * Created by Administrator on 2018/5/10/010.  
  8.  * 第二步:用于创建描述网络请求的接口  
  9.  */  
  10.   
  11. public interface GetRequest_Interface {  
  12.     @GET("ajax.php?a=fy&f=auto&t=auto&w=hello%20world")  
  13.     Call<Translation> getCall();  
  14.     // 注解里传入 网络请求 的部分URL地址  
  15.     // Retrofit把网络请求的URL分成了两部分:一部分放在Retrofit对象里,另一部分放在网络请求接口里  
  16.     // 如果接口里的url是一个完整的网址,那么放在Retrofit对象里的URL可以忽略  
  17.     // getCall()是接受网络请求数据的方法  
  18. }  

第四步:发送请求并解析的过程

[html]  view plain  copy
  1. package com.myjob;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8.   
  9. import com.google.gson.Gson;  
  10. import com.google.gson.GsonBuilder;  
  11.   
  12. import retrofit2.Call;  
  13. import retrofit2.Callback;  
  14. import retrofit2.Response;  
  15. import retrofit2.Retrofit;  
  16. import retrofit2.converter.gson.GsonConverterFactory;  
  17.   
  18. /**  
  19.  * 第三步:处理返回数据   http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=hello%20world  
  20.  */  
  21.   
  22. public class MainActivity extends AppCompatActivity {  
  23.     private Button button;  
  24.     private TextView textView;  
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.         initView();  
  30.   
  31.     }  
  32.     private void initView() {  
  33.         button=findViewById(R.id.btn);  
  34.         textView=findViewById(R.id.tv);  
  35.         button.setOnClickListener(new View.OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View view) {  
  38.                 request();//使用retrofit封装的方法  
  39.             }  
  40.         });  
  41.   
  42.     }  
  43.     private void request() {  
  44.         //第一步:创建一个retrofit的对象  
  45.         final Retrofit retrofitnew Retrofit.Builder()  
  46.                 .baseUrl("http://fy.iciba.com/")//设置网络请求 的URL  
  47.                 .addConverterFactory(GsonConverterFactory.create())//设置使用gson解析  
  48.                 .build();  
  49.         //第二步:创建请求接口的实例  
  50.         GetRequest_Interface getRequest_interfaceretrofit.create(GetRequest_Interface.class);  
  51.         //第三步:对发送请求进行封装  
  52.         Call<Translation>  call=getRequest_interface.getCall();  
  53.         //第四步:发送请求  
  54.         call.enqueue(new Callback<Translation>() {  
  55.             //第五步:请求成功回调  
  56.             @Override  
  57.             public void onResponse(Call<Translation> call, Response<Translation> response) {  
  58.   
  59.                 //处理返回的数据结果  
  60. //                response.body().show();结果可在控制台查看    一种方法  
  61.                 //第二种方法   解析了部分  
  62.   
  63.                 textView.setText("获取的到信息是:状态为"+response.body().getStatus()+"内容为:"+  
  64.                         response.body().getContent().getFrom()+","+response.body().getContent().getOut() + "等等");  
  65.             }  
  66.   
  67.             @Override//请求失败时回调  
  68.             public void onFailure(Call<Translation> call, Throwable t) {  
  69.                 //  
  70.                 System.out.println("请求失败");  
  71.             }  
  72.         });  
  73.     }  
  74. }  

第五步:简单的效果图


-----------------------------------------------------------------------------------------------------------------------------

参考文献:https://blog.csdn.net/carson_ho/article/details/73732076

项目源码:https://download.csdn.net/download/qq_36636969/10405724

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值