沈阳定制网站方案,做母亲节网站的素材,汕头人事考试网,功能型网站制作多少钱项目用已经使用了 Retrofit#xff0c;定义了接口方法#xff0c;返回了 JSON 转换后的实体对象#xff0c;炒鸡方便。但是总有意料之外的时候#xff0c;比如我不需要返回实体对象#xff0c;我要返回纯纯的 JSON 字符串#xff0c;怎么办呢#xff1f;
先看源码
通过…
项目用已经使用了 Retrofit定义了接口方法返回了 JSON 转换后的实体对象炒鸡方便。但是总有意料之外的时候比如我不需要返回实体对象我要返回纯纯的 JSON 字符串怎么办呢
先看源码
通过一系列的源码分析最后定位到 OkHttpCall 中的 parseResponse() 方法 下面代码中的 parseResponse 方法是纯复制过来的没改过可以看出当接口返回正确的数据之后无论如何都会调用 T body responseConverter.convert(catchingBody)把 JSON 字符串转换成了一个 T 对象我们没有办法通过配置什么东西来实现我们要返回纯 JSON 字符串的需求所以要想其他办法。两个办法1、让它转我们再转回来2、我们自己定义怎么转。
final class OkHttpCallT implements CallT {ResponseT parseResponse(okhttp3.Response rawResponse) throws IOException {ResponseBody rawBody rawResponse.body();// Remove the bodys source (the only stateful object) so we can pass the response along.rawResponse rawResponse.newBuilder().body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength())).build();int code rawResponse.code();if (code 200 || code 300) {try {// Buffer the entire body to avoid future I/O.ResponseBody bufferedBody Utils.buffer(rawBody);return Response.error(bufferedBody, rawResponse);} finally {rawBody.close();}}if (code 204 || code 205) {rawBody.close();return Response.success(null, rawResponse);}ExceptionCatchingResponseBody catchingBody new ExceptionCatchingResponseBody(rawBody);try {T body responseConverter.convert(catchingBody);return Response.success(body, rawResponse);} catch (RuntimeException e) {// If the underlying source threw an exception, propagate that rather than indicating it was// a runtime exception.catchingBody.throwIfCaught();throw e;}}
}方法一返回 JSONObject 后再转 JSON 字符串
这个很简单我们把返回实体类改成 JSONObject然后 Converter 会帮忙我们转成 JSONObject然后我们再转成字符串即可。缺点就是转了两轮。
// 接口定义
POST(xxx)
fun fetch(Body param: RequestBody): CallJSONObject// 使用
val response api.fetch(param).execute()
val json response.body()?.toJSONString() ?: 方法二自定义 Converter
模仿 FastJsonResponseBodyConverter 自定义一个 Converter直接返回字符串不转实体对象即可收工。
// 自定义Converter
// 挖坑理论上可以定义一个注解然后判断 annotations 中是否包含此注解
// 如果包含则返回自定义Converter否则返回原来的Converter。
.addConverterFactory(object : Converter.Factory() {override fun responseBodyConverter(type: Type,annotations: Arrayout Annotation,retrofit: Retrofit): ConverterResponseBody, String {return ConverterResponseBody, String { responseBody -responseBody.use { it.string() }}}
})// 接口定义
POST(xxx)
fun fetch(Body param: RequestBody): CallString// 使用
val response api.fetch(param).execute()
val json response.body() ?:
文章转载自: http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.3jiax.cn.gov.cn.3jiax.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.httzf.cn.gov.cn.httzf.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.stbfy.cn.gov.cn.stbfy.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.kgjyy.cn.gov.cn.kgjyy.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.tkrdg.cn.gov.cn.tkrdg.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.qbjrf.cn.gov.cn.qbjrf.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn