新版Retrofit 2可运行例子(解决Could not locate ResponseBody converter for问题)
Retrofit这东西我就不多做解释了,反正最近应用很广,基本都快和OkHttp一起成为安卓的事实网络访问标准框架了。
这么好的一个东西,官网文档实在是不算太好,说的不太清晰。按官网的经常会有“Could not locate ResponseBody converter for”问题。
反正折腾了一番,终于跑出来了一个例子。这里把正确的例子写出来,方便大家参考。
首先要注意的是Retrofit准确的说不是Anroid 的HttpClient,而是Java的HttpClient,所以用Java工程运行Demo的代码即可。
文中我自己搭建了一个spring 的rest服务,很简单,就是访问greeting/{name},会返回一个User对象,把name赋值给这个user对象。

public class User {
int id;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
1,依赖引入。
这个十分重要。。。这货升级了一下竟然改了包名,,,蛋疼。Maven,Gradle都行,Gradle的:
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.google.code.gson:gson:2.3'
Maven的:
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.0.0-beta3</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.0.0-beta3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
注意看,groupId是不一样的。如果想直接接收对象,那要引入gson和converter。不然就只能接收字符串。
2,写接口
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path; /**
* Created by csonezp on 16-1-29.
*/
public interface GreetingServicce {
@GET("/greeting/{name}")
Call<User> getGreeting(@Path("name") String name);
}
service里指定了访问的rest Api地址,路径变量,返回值。
3,构建Retrofit实例,访问网络。
这个自己随便建立个Java文件,将代码放进去就行。
import com.google.gson.Gson;
import retrofit2.Call;
import retrofit2.GsonConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit; import java.io.IOException; /**
* Created by csonezp on 16-1-29.
*/
public class Test {
public static void main(String[] args) throws IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.10.225:8080")
.addConverterFactory(GsonConverterFactory.create(new Gson())).build();
GreetingServicce servicce = retrofit.create(GreetingServicce.class);
Call<User> call = servicce.getGreeting("ss");
Response<User> response = call.execute();
System.out.print(response.body().toString());
}
}
注意红色部分,如果想直接接收到对象,则需要添加这个。
文中是同步访问网络的方式,要想在Android中使用,则需单开线程或者是换成异步访问。
现在运行你的项目就可以了。
把Spring文件也发出来吧:
import com.example.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*; import java.util.concurrent.atomic.AtomicLong; /**
* Created by csonezp on 16-1-21.
*/
@RestController
@EnableAutoConfiguration
public class SimpleController {
private final AtomicLong counter = new AtomicLong();
private static final String template = "Hello,%s!"; @RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
} @RequestMapping(value = "/greeting/{name}",method = RequestMethod.GET)
public User greeting(@PathVariable String name) {
return new User((int) counter.incrementAndGet(),
String.format(template, name));
} public static void main(String[] args) throws Exception {
SpringApplication.run(SimpleController.class, args);
}
}
新版Retrofit 2可运行例子(解决Could not locate ResponseBody converter for问题)的更多相关文章
- [经验] 新版SkyIAR、Easy Image X在有些PE里不能运行的解决办法
[经验] 新版SkyIAR.Easy Image X在有些PE里不能运行的解决办法 xxwl2008 发表于 2013-1-26 11:58:38 https://www.itsk.com/threa ...
- Windows Server 2008 任务计划无法自动运行的解决办法
问题:编写的bat脚本,直接执行,成功:但是在任务管理器中配置该任务,运行不成功,结果显示为:0x1,系统环境为 Windows Server 2008. 分析:bat任务没有调用执行. 解决方案: ...
- genymotion+Oracle VM VirtualBox + eclipse + appium 脚本运行慢解决步骤
genymotion+Oracle VM VirtualBox + eclipse + appium 脚本运行慢解决步骤 1.lenove 机器启动时按F1 进入bios 设置,设置cpu virtu ...
- DirectX11--教程项目无法编译、运行的解决方法
综述 对于Win10系统的大多数用户来说,可以直接编译本教程对应的项目并运行.但也有部分用户由于某些原因可能会出现无法编译的情况. DirectX11 With Windows SDK完整目录 欢迎加 ...
- [vuejs] 终端npm run dev 不能自动打开浏览器运行项目解决办法
终端执行: npm run dev 出现: I Your application is running here: http://localhost:8080 但并没有打开浏览器运行项目 解决办法: ...
- 【翻译自mos文章】job 不能自己主动运行的解决方法
job 不能自己主动运行的解决方法 參考原文: Jobs do not execute automatically (Doc ID 309945.1) 适用于: Oracle Server - Ent ...
- ProxyStrike运行bug解决办法
ProxyStrike运行bug解决办法 由于curl中参数CURLOPT_SSL_VERIFYHOST的值取消原有的值1,导致ProxyStrike无法正常运行.所以,要运行该工具,需要手动修改/ ...
- $ gulp watch 运行出错解决方法
$ gulp watch 运行出错解决方法 $ gulp watch 如果你出现了如下报错信息: gulp-notify: [Laravel Elixir] Browserify Fail ...
- 树莓派 Learning 002 装机后的必要操作 --- 05 给树莓派搭建“x86 + pi”环境 -- 安装**32位运行库** -- 解决`E:未发现软件包 xxx` 问题
树莓派 装机后的必要操作 - 给树莓派搭建"x86 + pi"环境 – 安装32位运行库 – 解决E:未发现软件包 xxx 问题 我的树莓派型号:Raspberry Pi 2 Mo ...
随机推荐
- Direct3D 10学习笔记(三)——文本输出
本篇将简单整理Direct3D 10的文本输出的实现,具体内容参照< Introduction to 3D Game Programming with DirectX 10>(中文版有汤毅 ...
- 字符串数组转为PHP级数组
先要把字符串处理一下,成为php定义数组的形式,再用eval执行: $str="Array([15] => Array([id] => 2304[fromtype] => ...
- Android Handler 最佳的理解资料
- Linux系统启动错误 contains a file system with errors, check forced解决方法
/dev/sda1 contains a file system with errors, check forced./dev/sda1: Inodes that were part of a cor ...
- sql语句变量定义和样例
变量和与常量 1.定义和使用局部变量说明:局部变量是用户可自定义的变量,它的作用范围仅在程序内部.局部变量的名称是用户自定义的,命名的局部变量名要符合SQL Server 2000标识符命名规则,必须 ...
- python数据结构
. 数据结构¶ .1. 深入列表¶ 链表类型有很多方法,这里是链表类型的所有方法: list.append(x) 把一个元素添加到链表的结尾,相当于 a[len(a):] = [x] . list ...
- linux sed的使用
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理, 可以将数据行进行替换.删除.新增.选取等特定工作. sed本质上是一个编辑器,但是它是非交互式的,这点与VIM不同:同时 ...
- Nginx Debug Log
//检查nginx.conf时(sudo ./nginx -t),输出数据到检测结果 //ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "配置解析处理&q ...
- git format-patch & git apply & git clean
一.打补丁 git format-patch & git apply 最近在工作中遇到打补丁的需求,一来觉得直接传文件有些low(而且我尝试了一下,差点把项目代码毁了) ,二来也是想学习一下, ...
- TCP SYN扫描学习笔记
1.TCP SYN包扫描主机状态的原理:tcp协议规定,当目标主机收到一个tcp syn 包时,若目标主机处于开放状态,会返回给源主机一个tcp ack 包(目的端口开放),或者向源主机发送一个tcp ...