这篇文章主要介绍了Android中Retrofit 2.0如何使用JSON进行数据交互,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。之前使用Retrofit都是将jsON串转
这篇文章主要介绍了Android中Retrofit 2.0如何使用JSON进行数据交互,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
之前使用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;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);}
POST方式,采用指定完整的URL,reqeustBody就是后面业务要传入的完整JSON串
GET方式,后面的options就是一个Map,业务参数键值就存在这个里面,URL里面不需要带值。
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;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; } 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()); } } }); } 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;public interface HttpCallback { void onSuccess(HttpResMsg httpResMsg); void onError(String errORMsg);}
消息结构的组装:
package com.hdnetworklib.network.http;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; }}
感谢你能够认真阅读完这篇文章,希望小编分享的“Android中Retrofit 2.0如何使用JSON进行数据交互”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: Android中Retrofit 2.0如何使用JSON进行数据交互
本文链接: https://www.lsjlt.com/news/221454.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0