什么是Android的等效NSURLProtocol从iOS SDK? [英] What is Android's equivalent for NSURLProtocol from the iOS SDK?

查看:1186
本文介绍了什么是Android的等效NSURLProtocol从iOS SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,它利用第三方jar。我想监视并可能修改由图书馆发出的HTTP请求。在iOS上,我可以做到这一点通过注册一个自定义的 NSURLProtocol ,可以检查和处理这种情况发生在应用程序的过程中的任何网络请求的类。

I'm developing an Android app that utilizes a third-party jar. I want to monitor and possibly modify the HTTP requests issued by that library. Under iOS, I can do that by registering a custom NSURLProtocol class that can inspect and manipulate any network requests that happen in the app's process.

有什么相似的Andr​​oid?我承认,我是新的Andr​​oid开发,但一直未能找到工作解决这个问题。

Is there anything similar for Android? I admit, I am new to Android development, but have been unable to find a working solution to this problem.

我没有源出于此罐子,我不能依靠反编译了。

I don't have the source for this jar, and I can't rely on decompiling it.

一对夫妇的事情我已经了解:

A couple of things I've explored:

  • 的Andr​​oid的ContentProvider 就不行,因为该解决方案必须支持。HTTP://的要求,而不是内容://请求
  • 创建自定义的 org.apache.http.impl.client.DefaultHTTPClient 类。(这是用于请求的底层网络类我后我。)看来,这是行不通的,因为Android已经缓存这类的系统版本,并总是加载的,而不是我的,即使它被设置在加载顺序较高。
  • Android ContentProvider. Won't work, since the solution must support "http://" requests, not "content://" requests.
  • Creating a custom org.apache.http.impl.client.DefaultHTTPClient class. (This is the underlying networking class used for the requests I'm after.) Seems that this won't work, since Android has cached the system version of this class and always loads that instead of mine, even though it is set higher in the load order.

我看过了Apache的HTTP库pretty的好,虽然它有各种各样的东西像的Htt prequestInterceptor 的Htt presponseInterceptor 类,不帮我多好,因为库实例化一个 DefaultHTTPClient 实例,并将其分配给在我没有访问私有方法私有成员。

I've looked over the Apache HTTP library pretty well, and while it has all sorts of things like HttpRequestInterceptor, HttpResponseInterceptor classes, that doesn't do me much good, since the library instantiates a DefaultHTTPClient instance and assigns it to a private member within a private method that I don't have access to.

在这里我的最终目标是监督和改变这些请求进行单元测试。我不想发出真正的请求;我想提供一个模拟的响应。

My ultimate goal here is to monitor and alter these requests for unit testing purposes. I don't want the real requests to be issued; I want to supply a mock response.

推荐答案

您是不幸的运气了:有那种没有应用范围的拦截框架提供给Android应用程序

You are unfortunately out of luck: there is no app-wide interception framework of that sort available to Android apps.

我的建议是设置一个代理服务器拦截请求,并提供你的模拟响应。

My suggestion would be to setup a proxy that intercepts the requests and supplies your mock response.

这篇关于什么是Android的等效NSURLProtocol从iOS SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆