What is Android equivalent for NSURLProtocol from iOS SDK?

I am developing an Android application that uses a third-party jar. I want to track and possibly modify the HTTP requests issued by this library. In iOS, I can do this by registering my own NSURLProtocol class, which can check and process any network requests that occur during the application.

Is there something similar for Android? I admit that I am new to Android development, but could not find a working solution to this problem.

I have no source for this jar, and I cannot rely on its decompilation.

A few things I learned:

  • Android ContentProvider. It will not work, since the solution should support requests "http: //", but not requests "content: //".
  • Creating a custom class org.apache.http.impl.client.DefaultHTTPClient . (This is the base network class used for requests that I ask them.) It does not seem to work because Android cached the system version of this class and always loads it instead of mine, even if it is installed above in boot order.

I looked pretty well at the Apache HTTP library, and although it has things like the HttpRequestInterceptor , HttpResponseInterceptor classes, this does not do me much good, because the library creates an instance of DefaultHTTPClient instance and assigns it to a private member in a private method, to which I do not have access.

My ultimate goal is to track and modify these requests for unit testing. I do not want these requests to be issued; I want to give a response.

+7
source share
1 answer

Unfortunately, you are out of luck: Android applications do not have applications for intercepting applications.

My suggestion was to set up a proxy server that intercepts requests and provides your mock response.

+2
source

All Articles