How to add a proxy server to okhttp within a singleton component?

418 Views Asked by At

In my dagger graph, I need to provide okhttp with proxy. I am using java.net.InetSocketAddress with java.net.Proxy which causes network on main thread exception.

@InstallIn(SingletonComponent::class)
@Module
class SingletonModule {
    @Provides
    fun proxyClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .proxy(
                Proxy(Proxy.Type.HTTP, 
                    InetSocketAddress("192.9.09.8", 98))
            ).callTimeout(2, TimeUnit.MINUTES).build()
    }
}

Suspend function doesn't seems to work with hilt.
Is there a way to use asynchronous code in Hilt?

Crash Logs

Caused by: android.os.NetworkOnMainThreadException        
   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
   at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
   at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
   at java.net.InetAddress.getAllByName(InetAddress.java:1152)
   at java.net.Socket.<init>(Socket.java:218)
   at com.moblie.frencheska.hilt.SingletonModule.proxyClient(SingletonModule.kt:55)
0

There are 0 best solutions below