Android中Retrofit 2.0直接使用JSON进行数据交互
作者:平凡至简 发布时间:2022-01-08 07:22:46
之前使用Retrofit都是将JSON串转化为POJO对象,针对不同的业务协议,定义相应的接口和参数列表。但是此种方式一般用在自己内部协议基础上,具体大的项目中,有些第三方的集成功能,一般都采用统一的方式即请求JSON和回应JSON进行数据交互,不可能每个第三方协议都会去定义与协议相应的POJO对象。
HTTP肯定有GET和POST方法,先定义Retrofit Api的interface:
package com.hdnetworklib.network.http;
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
*/
public interface RetrofitServiceApi {
@POST
Call<ResponseBody> reqPost(@Url String url, @Body RequestBody requestBody);
@GET
Call<ResponseBody> reqGet(@Url String url, @QueryMap Map<String, String> options);
@GET
Call<ResponseBody> reqGet(@Url String url);
}
1、POST方式,采用指定完整的URL,reqeustBody就是后面业务要传入的完整JSON串
2、GET方式,后面的options就是一个Map,业务参数键值就存在这个里面,URL里面不需要带值。
3、GET方式,与2不同的是没有options,这样就键值对全部带在URL里面,类似于这样的格式:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1
接下来就是具体对业务的接口了,提供POST和GET两个请求接口调用:
package com.hdnetworklib.network.http;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by wangyuhang@evergrande.cn on 2017/7/12 0012.
*/
public class HttpClient {
private static final String TAG = "HttpClient";
private static volatile HttpClient instance;
private HttpClient() {
}
public static HttpClient getInstance() {
if (instance == null) {
synchronized (HttpClient.class) {
if (instance == null) {
instance = new HttpClient();
}
}
}
return instance;
}
/**
* Http Post请求
*
* @param req_id 请求编号
* @param method 请求业务方法
* @param url 请求的URL
* @param jsonData POST需要所带参数(JSON串格式)
* @param callback 回调接口
*/
public void reqPostHttp(final int req_id, final String method, String url, String jsonData, final HttpCallback callback) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.what.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitServiceApi retrofitServiceApi = retrofit.create(RetrofitServiceApi.class);
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonData);
Call<ResponseBody> call = retrofitServiceApi.reqPost(url, body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String result = response.body().string();
Log.i(TAG, "reqPostHttp onResponse: " + result);
if (callback != null) {
callback.onSuccess(new HttpResMsg(req_id, method, result));
}
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "reqPostHttp onResponse exception: " + e.toString());
if (callback != null) {
callback.onError(e.toString());
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "reqPostHttp onFailure: " + t.toString());
if (callback != null) {
callback.onError(t.toString());
}
}
});
}
/**
* Http Get请求
*
* @param req_id 请求编号
* @param method 请求业务方法
* @param url 请求的URL
* @param options GET需要所带参数键值(如果URL里带有则不需要在此添加)
* @param callback 回调接口
*/
public void reqGetHttp(final int req_id, final String method, String url,
Map<String, String> options, final HttpCallback callback) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.what.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitServiceApi retrofitServiceApi = retrofit.create(RetrofitServiceApi.class);
Call<ResponseBody> call = null;
if (options == null) {
call = retrofitServiceApi.reqGet(url);
} else {
call = retrofitServiceApi.reqGet(url, options);
}
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String result = response.body().string();
Log.i(TAG, "reqPostHttp onResponse: " + result);
if (callback != null) {
callback.onSuccess(new HttpResMsg(req_id, method, result));
}
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "reqPostHttp onResponse exception: " + e.toString());
if (callback != null) {
callback.onError(e.toString());
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "reqPostHttp onFailure: " + t.toString());
if (callback != null) {
callback.onError(t.toString());
}
}
});
}
}
需要注意的是:
baseUrl(http://www.what.com/)
这里的这个baseUrl是我瞎掰的一个地址,因为Retrofit的限制:如果baseUrl不是以 / 结尾就会报异常:
Caused by: java.lang.IllegalArgumentException: baseUrl must end in /
当我们需要完整的指定URL的时候,特别是上面列出的第二种GET方式,我们的URL是http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1,如果我们直接通过接口传参把这个URL直接传入baseUrl中,如下(注意最后没有/结尾):
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1")
.addConverterFactory(GsonConverterFactory.create())
.build();
这样运行时就会报错。那如果我们手工在最后面加上一个/呢?如下(注意最后有/结尾):
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
这样运行时仍然报错,而且你把这个链接复制到浏览器中看看就知道肯定不行的:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/
我一开始遇到这个问题的时候也是第一反应去查Retrofit的官方文档和说明,或者让第三方的开发人员采用第二种GET请求方式,用一个以 / 结尾的URL,然后把URL中?后面带的那些值放到一个Map里传进来。首先官方说明和Api用法没找到,而且这个baseUrl还必须调用,其次,别的开发人员不愿意弄,好好的辛辛苦苦把URL都组装好了,没啥事让我传Map啊,肯定也不行。后面在这里找到了答案:https://stackoverflow.com/questions/36736854/retrofit2-how-do-i-put-the-at-the-end-of-the-dynamic-baseurl
所以既然你后面会完整指定URL,那么一开始的baseUrl就无关紧要,随便写一个以/结尾的Http地址就可以了。
剩下的的就是回调和消息的组装了,各位可以根据自己的业务需求进行组装和调整,我这里就只贴出代码不做过多解析了。
回调接口:
package com.hdnetworklib.network.http;
/**
* Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
*/
public interface HttpCallback {
void onSuccess(HttpResMsg httpResMsg);
void onError(String errorMsg);
}
消息结构的组装:
package com.hdnetworklib.network.http;
/**
* Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
*/
public class HttpResMsg {
private Integer req_id;
private String method;
private String data;
public HttpResMsg() {
}
public HttpResMsg(int req_id, String method, String data) {
this.req_id = req_id;
this.method = method;
this.data = data;
}
public Integer getReq_id() {
return req_id;
}
public void setReq_id(Integer req_id) {
this.req_id = req_id;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
来源:http://www.jianshu.com/p/1e5200898c7b?utm_source=tuicool&utm_medium=referral
猜你喜欢
- 一、输入映射parameterType指定输入参数的Java类型,可以使用别名或者类的全限定名。它可以接收简单类型、POJO、H
- 算法简介迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学迪家迪杰斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶
- 在实际工作中,重试重发请求处理是一个非常常见的场景,比如:1、发送消息失败。2、调用远程服务失败。3、争抢锁失败。这些错误可能是因为网络波动
- 在本文中,我们将介绍二进制搜索相对于简单线性搜索的优势,并介绍它在 Java 中的实现。1. 需要有效的搜索假设我们在wine-sellin
- 在工作中经常读写文本文件,在读文件时,需要按开头的两个字节判断文件格式,然后按该格式读文件中的内容。 写文件时,也要按目标文件指定
- 1.通过无参构造函数创建(默认)2.通过有参构造创建1.constructor 的index赋值<bean id="user
- 代码优化细节 1、尽量指定类、方法的final修饰符带有final修饰符的类是不可派生的。在Java核心API中,有许多应用fin
- Jackson反序列化遇到的问题最近在项目中需要使用Jackson把前台转来的字符转为对象,转换过程中发生了错误,报错如下com.faste
- 接着上一篇进行学习java文件上传下载1。五、断点续传 对于熟用QQ的程序员,QQ的断点续传功能应该是印象很深刻的。因为它很实用也
- 本文实例讲述了Android精灵动画用法。分享给大家供大家参考。具体如下:ElaineAnimated.java文件如下:package n
- --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1
- 本文实例讲述了C#使用oledb操作excel文件的方法。分享给大家供大家参考。具体分析如下:不管什么编程语言都会提供操作Excel文件的方
- [ThreadStatic] static char[]
- 概述从今天开始, 小白我将带大家开启 Jave 数据结构 & 算法的新篇章.栈栈 (Stack) 是一种运算受限的线性表, 遵循先进
- 目录阻塞队列简介java中的阻塞队列BlockQueue中方法阻塞队列的实现原理总结阻塞队列简介阻塞队列(BlockingQueue)首先是
- Android事件处理机制是基于Listener实现的,比如触摸屏相关的事件,是通过OnTouchListener实现的;而手势是通过OnG
- jdk * 和cglib * 实现及区别代理模式是一种设计模式,提供了对目标对象额外的访问方式,即通过代理对象访问目标对象,这样可以在不
- spring security用了也有一段时间了,弄过异步和多数据源登录,也看过一点源码,最近弄rest,然后顺便搭oauth2,前端用js
- 先来看看几种比较单一的方式:IMEI方式:TelephonyManager.getDeviceId():问题范围:只能支持拥有通话功能的设备
- 一、显示信息对话框:使用“JOptionPane.showMessageDialog”显示:图标对话框类型语法显示错误类型对话框showMe