Blog Infos
Author
Published
Topics
, ,
Author
Published

If you’re an Android developer, you might have faced times when you’re building a new feature, but the API you need isn’t ready yet. Or maybe you need to test different scenarios with various API responses. In these cases, you need a tool that makes it easy to change or replace HTTP responses for your app.

To handle these situations, you need something simple that lets you customize or fully change HTTP responses. When you search for tools, you’ll probably find popular ones like Charles or Requestly. These tools are great, but they have a big downside: they’re complicated to set up with proxies and certificates.

OkHttp Request Modifier Android Library

That’s where the OkHttp Request Modifier comes in. This easy-to-use Android library lets you change HTTP responses without all the setup hassle. By adding the Request Modifier to your project, you can easily modify response bodies and response codes as needed.

In this article, we’ll explore how Request Modifier works, its main features, and how you can use it to make your development process smoother and more efficient.

Installation

For the installation, you need to include the library in your app module’s build.gradle file

releaseImplementation 'io.nerdythings:okhttp-requests-modifier-no-op:1.0.1'
debugImplementation 'io.nerdythings:okhttp-requests-modifier:1.0.1'

and add an Interceptor to the OkHttpClient in the code

// for OkHttp
val builder = OkHttpClient.Builder()
if (BuildConfig.DEBUG) {
    builder.addInterceptor(OkHttpRequestModifierInterceptor(applicationContext))
}    
val client = builder.build()


// for Retrofit
val builder = OkHttpClient.Builder()
if (BuildConfig.DEBUG) {
    builder.addInterceptor(OkHttpRequestModifierInterceptor(applicationContext))
}    
val client = builder.build()
val retrofit = Retrofit.Builder()
        ......
        .client(client)
        .build()

For security reasons, I recommend enabling this interceptor only for DEBUG builds! Also, Proguard will cut it out in the release build.

Plugin capabilities

With the Request Modifier, you can easily change the response body and response code of your HTTP requests. The library even supports regular expressions (regex), allowing you to match and modify multiple requests at once. This gives you a flexible way to handle various scenarios.

Important: If you override a request, it won’t make a real call. Instead, it will immediately return your custom data.

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

No results found.

Jobs

Request with a custom response in the OkHttp Profiler

 

Usage

To define custom responses, follow these steps:

  1. Add the Library: Include the library in your project by updating your build.gradle file.
  2. Set Up the Interceptor: Set up the custom interceptor as described above.
  3. Access the Built-in Activity: From your debug menu, access the built-in Activity, which offers a user-friendly interface for managing your requests.
  4. Add and Modify Requests: This Activity lets you easily add and modify your requests, specifying the desired response bodies and codes.
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Column(
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier.fillMaxSize(),
            ) {
                ...
                Button(onClick = ::openSettings) {
                    Text(text = stringResource(id = R.string.open_modifier))
                }
                ...
            }
    }

    private fun openSettings() {
        startActivity(OkHttpProfilerSettingsActivity.getIntent(applicationContext))
    }

    ...
}

The intuitive UI makes it easy for developers to define and manage custom responses without diving deep into the code, boosting productivity and making debugging more efficient.

This article is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Using annotations in Kotlin has some nuances that are useful to know
READ MORE
blog
One of the latest trends in UI design is blurring the background content behind the foreground elements. This creates a sense of depth, transparency, and focus,…
READ MORE
blog
Now that Android Studio Iguana is out and stable, I wanted to write about…
READ MORE
blog
The suspension capability is the most essential feature upon which all other Kotlin Coroutines…
READ MORE
Menu