RAP介紹
用官方的話來說,RAP是一個可視化接口管理工具 通過分析接口結(jié)構(gòu),動態(tài)生成模擬數(shù)據(jù),校驗真實接口正確性, 圍繞接口定義,通過一系列自動化工具提升我們的協(xié)作效率。簡單來說就是用來mock后端的接口數(shù)據(jù),實現(xiàn)前后端模擬聯(lián)調(diào)。
在我們平時的開發(fā)工作中,經(jīng)常涉及到前后端的協(xié)同工作,往往前后端的進(jìn)度不一致,導(dǎo)致聯(lián)調(diào)的時間后延。當(dāng)然我們可以通過搭建web服務(wù)器來模擬接口數(shù)據(jù),這里介紹一種更簡單的調(diào)試方式,即通過阿里的RAP接口服務(wù)來實現(xiàn)接口mock。
RAP的簡單配置和使用
RAP官網(wǎng) http://rap.taobao.org/org/index.do, RAP官網(wǎng)提供了非常詳細(xì)的wiki和視頻教程。
1、注冊登錄
2、創(chuàng)建項目

3、創(chuàng)建好之后會在項目中建立一個默認(rèn)的模塊,點擊編輯后可以修改模塊、頁面、接口名稱

4、界面操作也比較簡單
如下圖所示,可以添加接口,并且設(shè)置接口的請求參數(shù)和響應(yīng)參數(shù),也可以直接導(dǎo)入json格式數(shù)據(jù)

添加接口

預(yù)覽mock數(shù)據(jù)

5、編輯好接口和參數(shù)之后,就可以點擊page右側(cè)的按鈕進(jìn)入控制臺調(diào)試界面

右下角會輸出mock的接口地址,也就是前端可以直接訪問的接口地址
android客戶端請求實現(xiàn)
下面就來看看android客戶端的請求實現(xiàn)
其實也就是簡單的網(wǎng)絡(luò)請求代碼
//獲取后端接口地址, 這里直接返回上面RAP控制臺輸出的接口地址
private String getServerUrl() {
Log.d(TAG, "getServerUrl " );
return "http://rap.taobao.org/mockjs/15968/getUserInfo?ver=2";
}
通過AsyncTask來執(zhí)行網(wǎng)絡(luò)請求,適用于簡單的網(wǎng)絡(luò)請求場景
//開始請求接口數(shù)據(jù)
public void request() {
asyncTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... tempParams) {
HttpURLConnection connection = null;
InputStream inputStream;
OutputStream outputStream = null;
BufferedInputStream bufferedInputStream = null;
ByteArrayOutputStream baos = null;
try {
URL urlObj = new URL(getServerUrl());
connection = (HttpURLConnection) urlObj.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
connection.connect();
// outputStream = connection.getOutputStream();
// outputStream.flush();
// outputStream.close();
int respCode = connection.getResponseCode();
if (respCode == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte[50 * 1024];
int length;
baos = new ByteArrayOutputStream();
while ((length = bufferedInputStream.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, length);
}
String result = new String(baos.toByteArray());
//結(jié)果解析
parseResult(result);
Log.d(TAG, "request result: " + result);
} else {
Log.d(TAG, "connect error " + respCode);
}
} catch (IOException e) {
Log.e(TAG, "", e);
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
Log.e(TAG, "error ", e);
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
Log.e(TAG, "error ", e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
Log.e(TAG, "error ", e);
}
}
if (connection != null) {
connection.disconnect();
}
}
return null;
}
};
Log.d(TAG, "start request...");
asyncTask.execute();
}
解析json數(shù)據(jù)
//結(jié)果解析
private void parseResult(String result){
try {
JSONObject jsonObject = new JSONObject(result);
String name = jsonObject.optString("name");
Log.d(TAG, "parseResult name " + name);
String id = jsonObject.optString("id");
Log.d(TAG, "parseResult id " + id);
String url = jsonObject.optString("url");
Log.d(TAG, "parseResult url " + url);
//字符編碼不一致則需要按指定格式解碼
// String strUTF8 = URLDecoder.decode(name, "UTF-8");
// Log.d(TAG, strUTF8);
}catch (Exception e){
}
}
總結(jié)
上述只是介紹了RAP接口工具的最簡單用法以及與android端的簡單聯(lián)調(diào)方式,未涉及多線程請求、加密壓縮、后端數(shù)據(jù)庫等,關(guān)于RAP的高級用法可以參考官網(wǎng)介紹。