首先需要导入这样两个包

 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. [leetcode]Flatten Binary Tree to Linked List @ Python

    原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...

  2. mongoDB报错Cannot find module '../build/Release/bson'

    打算用nodejs写一个blog系统,发现nodejs还是存在很多的坑.在使用mongodb时遇到如下报错问题: { [Error: Cannot find module '../build/Rele ...

  3. 遇到问题描述:Android Please ensure that adb is correctly located at问题解决

    遇到问题描述: 运行android程序控制台输出 [2013-11-04 16:18:26 - ] The connection to adb is down, and a severe error ...

  4. 数据库操作语句类型(DQL、DML、DDL、DCL)简介

    SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHER ...

  5. 如何:使用TreeView控件实现树结构显示及快速查询

    本文主要讲述如何通过使用TreeView控件来实现树结构的显示,以及树节点的快速查找功能.并针对通用树结构的数据结构存储进行一定的分析和设计.通过文本能够了解如何存储层次结构的数据库设计,如何快速使用 ...

  6. 用Main方法调用freemarker生成文件

    MyGenerator.java package com.comp.common; import java.io.BufferedWriter; import java.io.File; import ...

  7. SqlServer日常积累(一)

    1. 将一个表的数据插入另一个表 情况一:目标表已存在 (1)如果2张表的字段一致,并且希望插入全部数据,可以用这种方法: Insert Into 目标表 Select * From 来源表; --例 ...

  8. 教您使用java爬虫gecco抓取JD全部商品信息

    gecco爬虫 如果对gecco还没有了解可以参看一下gecco的github首页.gecco爬虫十分的简单易用,JD全部商品信息的抓取9个类就能搞定. JD网站的分析 要抓取JD网站的全部商品信息, ...

  9. Linux 小知识点

    1:当前目录比较重要的几个文件 . .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc install ...

  10. css 设置英文字母大小写转换(text-transform)

      css 设置英文字母大小写转换 CreateTime--2018年5月25日07点16分 Author:Marydon 1.实现:通过text-transform实现 2.text-transfo ...