博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Retroifit2
阅读量:7207 次
发布时间:2019-06-29

本文共 3480 字,大约阅读时间需要 11 分钟。

hot3.png

1、什么是Retrofit框架?

它是Square公司开发的现在非常流行的网络框架,所以我们在导入它的包的时候都可以看到这个公司的名字,目前的版本是2。

特点:

性能好,处理快,使用简单,Retrofit 是安卓上最流行的HTTP Client库之一

使用REST API设计风格
支持 NIO(new i/o)
默认使用OKHttp处理网络请求,我觉得可以看成是OKHttp的增强。
默认使用Gson解析

测试Url:

其中是BASE_URL,也就是基础地址,basil2style是GET的参数,如果访问成功会返回给我们一个json字符串:

{  "login": "basil2style",  "id": 1285344,  "avatar_url": "https://avatars.githubusercontent.com/u/1285344?v=3",  "gravatar_id": "",  "url": "https://api.github.com/users/basil2style",  "html_url": "https://github.com/basil2style",  "followers_url": "https://api.github.com/users/basil2style/followers",  "following_url": "https://api.github.com/users/basil2style/following{/other_user}",  "gists_url": "https://api.github.com/users/basil2style/gists{/gist_id}",  "starred_url": "https://api.github.com/users/basil2style/starred{/owner}{/repo}",  "subscriptions_url": "https://api.github.com/users/basil2style/subscriptions",  "organizations_url": "https://api.github.com/users/basil2style/orgs",  "repos_url": "https://api.github.com/users/basil2style/repos",  "events_url": "https://api.github.com/users/basil2style/events{/privacy}",  "received_events_url": "https://api.github.com/users/basil2style/received_events",  "type": "User",  "site_admin": false,  "name": "Basil",  "company": "MakeInfo",  "blog": "http://www.themakeinfo.com",  "location": "Peterborough,ON,Canada",  "email": "basiltalias92@gmail.com",  "hireable": true,  "bio": null,  "public_repos": 45,  "public_gists": 4,  "followers": 52,  "following": 145,  "created_at": "2011-12-26T00:17:22Z",  "updated_at": "2016-06-23T20:22:05Z"}
  • 1
  • 2
  • 3

好,按步骤开始写代码

1、定义一个接口(封装URL地址和数据请求)

RequestServices.java

package com.example.eventbus.retrofittest;import okhttp3.ResponseBody;import retrofit2.Call;import retrofit2.http.GET;/** * Created by LHD on 2016/6/25. */public interface RequestServices {    //请求方式为GET,参数为basil2style,因为没有变量所以下面getString方法也不需要参数    @GET("basil2style")     //定义返回的方法,返回的响应体使用了ResponseBody    Call
getString();}
  • 1
  • 2
  • 3

我们通常把基础地址都放在一个类里,方便调用

Constant.java

package com.example.eventbus.retrofittest;/** * Created by LHD on 2016/6/25. */public class Constant {    //baseurl    public final static String URL_BASE = "https://api.github.com/users/";}
  • 1
  • 2
  • 3

2、实例化Retrofit

//获取Retrofit对象,设置地址        Retrofit retrofit = new Retrofit.Builder()                .baseUrl(Constant.URL_BASE)                .build();
  • 1
  • 2
  • 3

3、通过Retrofit实例创建接口服务对象

RequestServices requestServices = retrofit.create(RequestServices.class);
  • 1

4、接口服务对象调用接口中方法,获得Call对象

Call
call = requestServices.getString();
  • 1

5、Call对象执行请求(异步、同步请求)

call.enqueue(new Callback
() { @Override public void onResponse(Call
call, Response
response) { if (response.isSuccess()){ try { Log.i("LHD",response.body().toString()); //返回的结果保存在response.body()中 String result = response.body().string(); //onResponse方法是运行在主线程也就是UI线程的,所以我们可以在这里 //直接更新UI textView.setText(result); } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(Call
call, Throwable t) { Log.i("LHD","访问失败"); } });

转载于:https://my.oschina.net/u/3730650/blog/1591332

你可能感兴趣的文章
React组件常用设计模式之Render Props
查看>>
多多客DOODOOKE更新插件&模块及下载附件教程
查看>>
js简单倒计时
查看>>
手把手教你React(一)JSX与虚拟DOM
查看>>
snabbdom源码解析(七) 事件处理
查看>>
在北京做Java开发如何月薪达到两万,需要技术水平达到什么程度?
查看>>
移动端适配之二:visual viewport、layout viewport和ideal viewport介绍
查看>>
python大佬养成计划----flask_sqlalchemy操作数据库
查看>>
Chrome开发者工具关于网络请求的一个隐藏技能
查看>>
Git入门与开发
查看>>
Java编程基础04——流程控制语句
查看>>
vue-threeJS数据驱动的三维图形可视化
查看>>
Ubuntu 18.04.1 搭建Java环境和HelloWorld
查看>>
Flutter 实现根据环境加载不同配置
查看>>
浏览器保存密码后自动填充问题
查看>>
前端每日实战:93# 视频演示如何用纯 CSS 创作一根闪电连接线
查看>>
PhpStorm升级后调用某些类提示phpstorm Unhandled exceptions
查看>>
Python 2.x 与 Python 3.x 的区别
查看>>
如何实现对tcl脚本的类GDB调试
查看>>
基于java的IO流的文件读取系统
查看>>