android模拟接口测试,利用RAP实现android前后端接口简单mock测试

RAP介绍

用官方的话来说,RAP是一个可视化接口管理工具 通过分析接口结构,动态生成模拟数据,校验真实接口正确性, 围绕接口定义,通过一系列自动化工具提升我们的协作效率。简单来说就是用来mock后端的接口数据,实现前后端模拟联调。

在我们平时的开发工作中,经常涉及到前后端的协同工作,往往前后端的进度不一致,导致联调的时间后延。当然我们可以通过搭建web服务器来模拟接口数据,这里介绍一种更简单的调试方式,即通过阿里的RAP接口服务来实现接口mock。

RAP的简单配置和使用

1、注册登录

2、创建项目

20c34545d3fc

QQ图片20170327221326.png

3、创建好之后会在项目中建立一个默认的模块,点击编辑后可以修改模块、页面、接口名称

20c34545d3fc

QQ图片20170327221358.png

4、界面操作也比较简单

如下图所示,可以添加接口,并且设置接口的请求参数和响应参数,也可以直接导入json格式数据

20c34545d3fc

QQ图片20170327222105.png

添加接口

20c34545d3fc

QQ图片20170327222447.png

预览mock数据

20c34545d3fc

QQ图片20170327222208.png

5、编辑好接口和参数之后,就可以点击page右侧的按钮进入控制台调试界面

20c34545d3fc

QQ图片20170327222415.png

右下角会输出mock的接口地址,也就是前端可以直接访问的接口地址

android客户端请求实现

下面就来看看android客户端的请求实现

其实也就是简单的网络请求代码

//获取后端接口地址, 这里直接返回上面RAP控制台输出的接口地址

private String getServerUrl() {

Log.d(TAG, "getServerUrl " );

return "http://rap.taobao.org/mockjs/15968/getUserInfo?ver=2";

}

通过AsyncTask来执行网络请求,适用于简单的网络请求场景

//开始请求接口数据

public void request() {

asyncTask = new AsyncTask() {

@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());

//结果解析

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数据

//结果解析

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){

}

}

总结

上述只是介绍了RAP接口工具的最简单用法以及与android端的简单联调方式,未涉及多线程请求、加密压缩、后端数据库等,关于RAP的高级用法可以参考官网介绍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值