Android中Retrofit 2.0直接使用JSON進行數(shù)據(jù)交互
之前使用Retrofit都是將JSON串轉(zhuǎn)化為POJO對象,針對不同的業(yè)務(wù)協(xié)議,定義相應(yīng)的接口和參數(shù)列表。但是此種方式一般用在自己內(nèi)部協(xié)議基礎(chǔ)上,具體大的項目中,有些第三方的集成功能,一般都采用統(tǒng)一的方式即請求JSON和回應(yīng)JSON進行數(shù)據(jù)交互,不可能每個第三方協(xié)議都會去定義與協(xié)議相應(yīng)的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就是后面業(yè)務(wù)要傳入的完整JSON串
2、GET方式,后面的options就是一個Map,業(yè)務(wù)參數(shù)鍵值就存在這個里面,URL里面不需要帶值。
3、GET方式,與2不同的是沒有options,這樣就鍵值對全部帶在URL里面,類似于這樣的格式:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1
接下來就是具體對業(yè)務(wù)的接口了,提供POST和GET兩個請求接口調(diào)用:
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 請求業(yè)務(wù)方法
* @param url 請求的URL
* @param jsonData POST需要所帶參數(shù)(JSON串格式)
* @param callback 回調(diào)接口
*/
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 請求業(yè)務(wù)方法
* @param url 請求的URL
* @param options GET需要所帶參數(shù)鍵值(如果URL里帶有則不需要在此添加)
* @param callback 回調(diào)接口
*/
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不是以 / 結(jié)尾就會報異常:
Caused by: java.lang.IllegalArgumentException: baseUrl must end in /
當(dāng)我們需要完整的指定URL的時候,特別是上面列出的第二種GET方式,我們的URL是http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1,如果我們直接通過接口傳參把這個URL直接傳入baseUrl中,如下(注意最后沒有/結(jié)尾):
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1")
.addConverterFactory(GsonConverterFactory.create())
.build();
這樣運行時就會報錯。那如果我們手工在最后面加上一個/呢?如下(注意最后有/結(jié)尾):
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
這樣運行時仍然報錯,而且你把這個鏈接復(fù)制到瀏覽器中看看就知道肯定不行的:http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/
我一開始遇到這個問題的時候也是第一反應(yīng)去查Retrofit的官方文檔和說明,或者讓第三方的開發(fā)人員采用第二種GET請求方式,用一個以 / 結(jié)尾的URL,然后把URL中?后面帶的那些值放到一個Map里傳進來。首先官方說明和Api用法沒找到,而且這個baseUrl還必須調(diào)用,其次,別的開發(fā)人員不愿意弄,好好的辛辛苦苦把URL都組裝好了,沒啥事讓我傳Map啊,肯定也不行。后面在這里找到了答案:https://stackoverflow.com/questions/36736854/retrofit2-how-do-i-put-the-at-the-end-of-the-dynamic-baseurl


所以既然你后面會完整指定URL,那么一開始的baseUrl就無關(guān)緊要,隨便寫一個以/結(jié)尾的Http地址就可以了。
剩下的的就是回調(diào)和消息的組裝了,各位可以根據(jù)自己的業(yè)務(wù)需求進行組裝和調(diào)整,我這里就只貼出代碼不做過多解析了。
回調(diào)接口:
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);
}
消息結(jié)構(gòu)的組裝:
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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android網(wǎng)絡(luò)請求框架Retrofit詳解
- Android Retrofit 2.0框架上傳圖片解決方案
- 簡略分析Android的Retrofit應(yīng)用開發(fā)框架源碼
- Android app開發(fā)中Retrofit框架的初步上手使用
- Retrofit和OkHttp如何實現(xiàn)Android網(wǎng)絡(luò)緩存
- Android Retrofit2網(wǎng)路編程實現(xiàn)方法詳解
- Android Retrofit2數(shù)據(jù)解析代碼解析
- Android中Retrofit的簡要介紹
- 基于Retrofit2+RxJava2實現(xiàn)Android App自動更新
- Android retrofit上傳文件實例(包含頭像)
- Android 封裝Okhttp+Retrofit+RxJava,外加攔截器實例
- Android Retrofit 中文亂碼問題的解決辦法
- Android使用 Retrofit 2.X 上傳多文件和多表單示例
- Android Retrofit框架的使用
相關(guān)文章
Kotlin學(xué)習(xí)教程之協(xié)程Coroutine
這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)教程之協(xié)程Coroutine的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Android自定義View實現(xiàn)抖音飄動紅心效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)抖音飄動紅心效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Android 使用ViewPager實現(xiàn)左右循環(huán)滑動及輪播效果
ViewPager是一個常用的Android組件,不過通常我們使用ViewPager的時候不能實現(xiàn)左右無限循環(huán)滑動,在滑到邊界的時候會看到一個不能翻頁的動畫,可能影響用戶體驗,接下來通過本文給大家介紹Android 使用ViewPager實現(xiàn)左右循環(huán)滑動及輪播效果,一起看看吧2017-02-02

