android 数据抓包,模拟响应

本文介绍了在Android中进行数据抓包的方法,包括使用Fiddler和Charles等工具,并详细阐述了如何利用Mocky.io模拟HTTP响应。同时,通过OkHttp的Interceptor实现对特定API的拦截并返回自定义的响应数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 常用抓包软件fidder4   Charles(可以拦截跳转map功能)  等,
http://www.mocky.io/ 用于模拟http 网络请求返回

Map From 为需要拦截的接口,Map To 为模拟的api接口,此处我们填入 http://www.mocky.io/v2/58592298240000ba087c5a92,如下图:

需要等api 时候使用,
使用okhttp拦截器,
public class MockInterceptor  implements Interceptor {
    @Override
    public Response  intercept(Chain chain)  throws IOException {
        Gson gson =  new Gson() ;
        Response response  =  null;
        Response.Builder builder =  new Response.Builder()
                .code( 200)
                .message( "")
                .request(chain.request())
                .protocol(Protocol. HTTP_1_0)
                .addHeader( "content-type" "application/json") ;
        Request request = chain.request() ;
        if(request.url().equals( "we need to mock")){
            String respondString =  "{ \n +
                    "    \" code \" : \" 200 \" , \n +
                    "    \" message \" : \" success \"\n +
                    "    \" data \" :{ \n +
                    "        \n +
                    "    } \n +
                    "}" ;
            builder.body(ResponseBody. create(MediaType. parse( "application/json") ,respondString.getBytes())) ;
            response = builder.build() ;
        } else {
            response = chain.proceed(request) ;
        }
        return response ;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值