Skip to content

Use API REST interface to authenticate user on android app

I am writing an App with friends that uses an API to login. I am using OKHTTP for the GET/POST Requests. I have written the following code for the Login page:

private void loginpage() {
    final Button button = findViewById(;
    button.setOnClickListener(v -> {
        String username = findViewById(;
        String password = findViewById(;

        try {
            webUntisAPI.login(username,password,"Spengergasse"); //Accesses the API to login
        }catch (RuntimeException ignored){
             //On fail don't switch view


After trying to login I get following error message

W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(
        at okhttp3.Dns$Companion$DnsSystem.lookup(Dns.kt:49)
        at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:164)
        at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:129)
        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:205)
        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
W/System.err:     at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
        at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
        at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
W/System.err:     at com.example.myapplication.WebUntisAPI.login(
        at com.example.myapplication.MainActivity.lambda$loginpage$0$MainActivity(
        at com.example.myapplication.-$$Lambda$MainActivity$f4xelUsFrwZaMU-fM7MXdfynjRQ.onClick(Unknown Source:2)
        at android.view.View.performClick(
        at android.view.View.performClickInternal(
W/System.err:     at android.view.View.access$3600(
        at android.view.View$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.

I have tried multithreading but I have not found a solution that awaits the result of the login. And regarding the Strict Mode error at the beginning of the error message, I have added the following lines to my onCreate() method in my MainActivity class:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

I would appreciate it a lot if you could help me find a solution for this! Thank you!



The application may be doing too much work on its main thread.

What this error means is you can’t run api calls in main Thread, it’s only used to update UI.

Your setContentView(R.layout.activity_main); should be in the OnCreate not here. That should run first.

After ur UI is set you should run the API inside a coroutine

Also please have a try at using retrofit as its the recommended library by google for api calls

The best practice to running api calls is using coroutines it will make your app faster, i would suggest you to learn it, its awesome and easy to understand (worth it). heres a link to learn