首先需要导入这样两个包

 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使用初探的更多相关文章

  1. 初探领域驱动设计(2)Repository在DDD中的应用

    概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...

  2. CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探

    CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码 ...

  3. Retrofit2.0起步篇

    retrofit 英文名字是改装的意思,也就是说他是对网络请求的一种改装,他不负责进行网络请求,他是对请求方式的一种封装.真正进行网络请求的是okhttp. 以下所有内容在Android Studio ...

  4. 从273二手车的M站点初探js模块化编程

    前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数 ...

  5. JavaScript学习(一) —— 环境搭建与JavaScript初探

    1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...

  6. .NET文件并发与RabbitMQ(初探RabbitMQ)

    本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址:http://www.cnblogs.com/tdws/p/5860668.html 想必MQ这两个字母对于各位前辈们和老司 ...

  7. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  8. Android 网络框架之Retrofit2使用详解及从源码中解析原理

    就目前来说Retrofit2使用的已相当的广泛,那么我们先来了解下两个问题: 1 . 什么是Retrofit? Retrofit是针对于Android/Java的.基于okHttp的.一种轻量级且安全 ...

  9. 【手把手教你全文检索】Apache Lucene初探

    PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...

随机推荐

  1. statickeyword

    static词义:静态的,可以用于修饰变量和方法,static方法块可以优先于构造函数运行. 被static修饰的变量,叫静态变量,静态变量在内存中仅仅有一份拷贝 public static Stri ...

  2. [Node.js] Stream all things!

    Node.js come alone with many Stream API. Stream is useful when handling large trunck of data. For ex ...

  3. 避免闪烁的方法(OnEraseBkgnd)

    在图形图象处理编程过程中,双缓冲是一种主要的技术.我们知道,假设窗口在响应WM_PAINT消息的时候要进行复杂的图形处理,那么窗口在重绘时因为过频的刷新而引起闪烁现象. 解决这一问题的有效方法就是双缓 ...

  4. iOS UIApplication的代理方法总结

    1.简单介绍 1> 整个应用程序的象征,一个应用程序就一个UIApplication对象.使用了单例设计模式 2> 通过[UIApplication sharedApplication]訪 ...

  5. 打通Fedora19的vsftpd服务

    Fedora19默认vsftpd也没安,安装界面里也没地方让我选,这一点不如以前的版本人性化.没办法只有自己来了,倒也不费事. 1.下载vsftpd rpm安装包 我是从http://rpmfind. ...

  6. C语言打印字母金字塔(第一行是A 第二行是ABA ……)

    #include <stdio.h> #include <stdlib.h> int main() { int line;//代表行数 int i; char letter,c ...

  7. shiro实现基于机构加username的验证以及rememberMe

    一.Shiro的一些经验与rememberMe实现原理 Shiro的登录(Authorization)和验权(Authentication).默认都是依据usernameUserName来做验证和授权 ...

  8. 使用第三方控件DotNetBar来美化程序

    VS的控件确实有点丑陋,需要美化一下.我最先接触的就是DotNetBar,一直用它,一般都还稳定.下面简单地讲解一下使用方法 1. 下载破解版DotNetBar 10版本:http://www.cr1 ...

  9. ASP.NET伪静态

    http://www.duote.com/tech/5/14543.html 三.伪静态的坏处 当然犹如一篇文章的作者所说的:"如果流量稍大一些使用伪静态就出现CPU使用超负荷,我的同时在线 ...

  10. vscode - 设置中文语言

    记得上次安装的时候,自动提示安装本地语言包,现在的版本貌似不会了吧. 1.先安装扩展,按键CTRL+SHIFT+P 输入 ext install ,最后输入:language,大概就可以找到简体中文包 ...