首先需要导入这样两个包

 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. go语言基础之append扩容特点

    1.append扩容特点 示例: package main //必须有个main包 import "fmt" func main() { //如果超过原来的容量,通常以2倍容量扩容 ...

  2. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  3. Godaddy ssl续费更新问题总结

    之前客户在Godaddy 上购买的ssl证书过期了,但客户续费后打开https时却提示证书过期了 进行Godaddy 后台看到证书确实是过期的 但在账户里也确实看到ssl续费成功了 猜想可能是ssl续 ...

  4. Boosted Tree

    原文:http://www.52cs.org/?p=429 作者:陈天奇,毕业于上海交通大学ACM班,现就读于华盛顿大学,从事大规模机器学习研究. 注解:truth4sex  编者按:本文是对开源xg ...

  5. java 正则表达式获得html字符串中<img src>中的src中的url地址

    public static Set<String> getImgStr(String htmlStr) { Set<String> pics = new HashSet< ...

  6. JavaScript 与 PHP 的语法区别

    1. 基本类型 php的基本类型分别: .基本数据类型:整型.小数(float/double).字符串.布尔类型 .复合类型:数组.对象 .特殊类型:Null,资源类型 JavaScript的基本类型 ...

  7. MySQL监控主要指标及采集方法

    MySQL监控属于DB监控的模块之一,包括采集.展示.监控告警.本文主要介绍MySQL监控的主要指标和采集方法. MySQL监控和Redis监控的逻辑类似,可参考文章<Redis监控主要指标及采 ...

  8. 火狐浏览器Firefox如何使用插件,火狐有哪些好用的插件

    1 CoorPreviews 不打开网页链接预览该网页的内容. 预览如图所示: 点击关闭旁边的钉子可以让该窗口保持开着并且浏览速度加快.这对于快速浏览图片时非常有用. 2 FoxTab 3D方式预览网 ...

  9. Android 视频缩放/放大

    1. 原理 不直接改变Codec输出的视频宽高比,而是改变视频播放器窗体的大小. 2. 设置Window 须要将Window设置未能够超出屏幕尺寸 mWindow.setFlags(WindowMan ...

  10. leetcode Wildcard Matching greedy algrithm

    The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...