Retrofit2使用初探
首先需要导入这样两个包
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
写model,这里参照的是淘宝ip库,地址类似这种,http://ip.taobao.com/service/getIpInfo.php?xxx.xxx.xxx.xxx,最后xxx为你要查询的ip
json有了,写model,可以在JSON字符串转换成Java实体类(POJO)这个网站中将json转成java实体类,对于这个json,实体类转化出来是这个样子
IPModel.java
public class IPModel {
private int code;
private IPDataModel data;
public void setCode(int code){
this.code = code;
}
public int getCode(){
return this.code;
}
public void setData(IPDataModel data){
this.data = data;
}
public IPDataModel getData(){
return this.data;
}
}
IPDataModel.java
public class IPDataModel {
private String country;
private String country_id;
private String area;
private String area_id;
private String region;
private String region_id;
private String city;
private String city_id;
private String county;
private String county_id;
private String isp;
private String isp_id;
private String ip;
public void setCountry(String country){
this.country = country;
}
public String getCountry(){
return this.country;
}
public void setCountry_id(String country_id){
this.country_id = country_id;
}
public String getCountry_id(){
return this.country_id;
}
public void setArea(String area){
this.area = area;
}
public String getArea(){
return this.area;
}
public void setArea_id(String area_id){
this.area_id = area_id;
}
public String getArea_id(){
return this.area_id;
}
public void setRegion(String region){
this.region = region;
}
public String getRegion(){
return this.region;
}
public void setRegion_id(String region_id){
this.region_id = region_id;
}
public String getRegion_id(){
return this.region_id;
}
public void setCity(String city){
this.city = city;
}
public String getCity(){
return this.city;
}
public void setCity_id(String city_id){
this.city_id = city_id;
}
public String getCity_id(){
return this.city_id;
}
public void setCounty(String county){
this.county = county;
}
public String getCounty(){
return this.county;
}
public void setCounty_id(String county_id){
this.county_id = county_id;
}
public String getCounty_id(){
return this.county_id;
}
public void setIsp(String isp){
this.isp = isp;
}
public String getIsp(){
return this.isp;
}
public void setIsp_id(String isp_id){
this.isp_id = isp_id;
}
public String getIsp_id(){
return this.isp_id;
}
public void setIp(String ip){
this.ip = ip;
}
public String getIp(){
return this.ip;
}
}
接下来写实现IPUtils.java
public class IPUtils {
static final String URL = "http://ip.taobao.com/service/";
public interface IPService{
@GET("getIpInfo.php")
Call<IPModel> getIpMsg(@Query("ip")String ip);
}
static Retrofit retrofit = new Retrofit.Builder().baseUrl(URL).addConverterFactory(GsonConverterFactory.create()).build();
public static IPService ipService = retrofit.create(IPService.class);
}
好了,现在可以用了,在activity中使用
Call<IPModel> call = IPUtils.gitHubService.getIpMsg("这里填写我的ip");
call.enqueue(new Callback<IPModel>() {
@Override
public void onResponse(Call<IPModel> call, Response<IPModel> response) {
//这里的response就可以提取数据了
Log.i(TAG, response.body().getData().getCountry());
}
@Override
public void onFailure(Call<IPModel> call, Throwable t) {
// Log.e("MainActivity", t.toString());
}
});
Retrofit2使用初探的更多相关文章
- 初探领域驱动设计(2)Repository在DDD中的应用
概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...
- CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探
CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码 ...
- Retrofit2.0起步篇
retrofit 英文名字是改装的意思,也就是说他是对网络请求的一种改装,他不负责进行网络请求,他是对请求方式的一种封装.真正进行网络请求的是okhttp. 以下所有内容在Android Studio ...
- 从273二手车的M站点初探js模块化编程
前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数 ...
- JavaScript学习(一) —— 环境搭建与JavaScript初探
1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...
- .NET文件并发与RabbitMQ(初探RabbitMQ)
本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址:http://www.cnblogs.com/tdws/p/5860668.html 想必MQ这两个字母对于各位前辈们和老司 ...
- React Native初探
前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...
- Android 网络框架之Retrofit2使用详解及从源码中解析原理
就目前来说Retrofit2使用的已相当的广泛,那么我们先来了解下两个问题: 1 . 什么是Retrofit? Retrofit是针对于Android/Java的.基于okHttp的.一种轻量级且安全 ...
- 【手把手教你全文检索】Apache Lucene初探
PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...
随机推荐
- JavaScript使用技巧精萃
(一).确认删除用法: 1. BtnDel.Attributes.Add("onclick","return confirm('"+"确认删除?& ...
- 利用CSS、JavaScript及Ajax实现图片预加载的三大方法及优缺点分析
预加载图片是提高用户体验的一个很好方法.图片预先加载到浏览器中,访问者便可顺利地在你的网站上冲浪,并享受到极快的加载速度.这对图片画廊及图片占据很大比例的网站来说十分有利,它保证了图片快速.无缝地发布 ...
- Mac 显示和隐藏文件
显示和隐藏文件 显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令: def ...
- vs2013 密钥_
vs2013 密钥 最佳答案: BWG7X-J98B3-W34RT-33B3R-JVYW9
- (LeetCode 189)Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- es6(const、let)
首先我很好奇,明明现代浏览器支持ES6.ES7不是那么好,为何还推行? var let const 有何区别?它们之间的限制又是什么?且听我娓娓道来 1. var 和let的关系,为何推荐let? ( ...
- Linux 驱动之内核定时器
1.定时器 之前说过两类跟时间相关的内核结构. 1.延时:通过忙等待或者睡眠机制实现延时. 2.tasklet和工作队列,通过某种机制使工作推后运行,但不知道运行的详细时间. 接下来要介绍的定时器,可 ...
- faster rcnn测试VOC2012的问题
Traceback (most recent call last): File "./tools/test_net.py", line 90, in test_net(net, i ...
- isearch5 index,attribute和summary。
索引 isearch5 支持的索引分为:index,attribute和summary. Index指的是倒排索引,它存储了存储了从term到DocID的映射关系,形如: term-->(Doc ...
- oracle 快速备份表数据
oracle 快速备份表数据 CreateTime--2018年2月28日17:04:50 Author:Marydon UpdateTime--2017年1月20日11:45:07 1.1.9. ...