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(R.id.button4); button.setOnClickListener(v -> { String username = findViewById(R.id.editTextTextPassword).toString(); String password = findViewById(R.id.editTextTextPersonName).toString(); try { webUntisAPI.login(username,password,"Spengergasse"); //Accesses the API to login setContentView(R.layout.activity_main); mainpage(); }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(StrictMode.java:1600) 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 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.RouteSelector.next(RouteSelector.kt:71) 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(WebUntisAPI.java:49) at com.example.myapplication.MainActivity.lambda$loginpage$0$MainActivity(MainActivity.java:56) at com.example.myapplication.-$$Lambda$MainActivity$f4xelUsFrwZaMU-fM7MXdfynjRQ.onClick(Unknown Source:2) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:7425) W/System.err: at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28296) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) W/System.err: at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 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(); StrictMode.setThreadPolicy(policy);
I would appreciate it a lot if you could help me find a solution for this! Thank you!
Advertisement
Answer
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 https://zaidzakir.medium.com/coroutines-in-android-explained-8f00a37c8528