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: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...
随机推荐
- pymysql的使用心得(1)------小细节,注意!
最近一段时间开始使用MySQL,使用的是pymysql库. 其中遇到过一些小问题,值得记录一下,以便今后使用的时候注意到. 表格的建立,代码如下: cursor.execute("creat ...
- IOS系统之蓝牙外接设备
Ios系统对于蓝牙外接设备在iphone4以前都是蓝牙2.0的时候,需要通过苹果的审核,据统计通过率仅有2%左右,现在蓝牙2.0基本上处于淘汰状态,所以在这里就不考虑了. 现在iphone4s以后的设 ...
- Nginx配置文件(nginx.conf)配置具体解释
欢迎扫码增加Java高知群交流 Nginx的配置文件nginx.conf配置具体解释例如以下: user nginx nginx ; Nginx用户及组:用户 组. window下不指定 wo ...
- 解决Windows Git Bash中文乱码问题
在git 安装目录 etc 下面 添加以下配置信息 1,/etc/gitconfig: [gui] encoding = utf-8 #代码库统一用urf-8,在git gui中可以正常显示中文 [i ...
- Android实现Material Design风格的设置页面(滑动开关控件)
前言 本文链接 http://blog.csdn.net/never_cxb/article/details/50763271 转载请注明出处 參考了这篇文章 Material Design 风格的设 ...
- ArcGIS Pro体验01——申请、下载、安装
ArcGIS Pro采用了Ribbon界面风格,看起来好漂亮,听起来很强大,就是不知道用起来怎么样,在网上看到一个ArcGIS Pro Beta2版本,下载下来,安装启动,好眼熟,像Office201 ...
- PHP高级教程-JSON
PHP JSON 本章节我们将为大家介绍如何使用 PHP 语言来编码和解码 JSON 对象. 环境配置 在 php5.2.0 及以上版本已经内置 JSON 扩展. JSON 函数 函数 描述 json ...
- webpack插件html-webpack-plugin
1.插件安装 npm install html-webpack-plugin --save-dev 2.插件使用 webpack.config.js配置文件为: var htmlWebpackPlug ...
- 解决Unable to load component class org.sonar.scanner.report.ActiveRulesPublisher/Unable to load component interface org.sonar.api.batch.rule.ActiveRules: NullPointerException
解决办法 Delete the directory data/es in your SonarQube installation. Restart SonarQube.
- python之函数用法__setattr__
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__setattr__ #http://www.cnblogs.com/hongfei ...