0

I have simple "gadget" based on Arduino. It is connected to the wlan of my house and sending web requests to it turns on/off a red LED indicating that I'm busy/free in my room. This works fine when called from a browser or from my small winforms application. It is called synchronous, it is just enough, no need for async calls.

But, I tried to create a android app that sends the same requests, no success. I have read many topics, tried different ways etc. The app has this row in manifest file:

    android:networkSecurityConfig="@xml/network_security_config"

Maybe something is still missing. This might be very very basic, but I haven't been able to get it working. Code:

   public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                OkHttpClient client = new OkHttpClient();
                String url = "http://192.168.100.12/LED=ON";

                Request req = new Request.Builder()
                        .url(url)
                        .build();

                try {
                    client.newCall(req).execute();
                } catch (IOException | RuntimeException e) {
                    e.printStackTrace();
                }

                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
            }
        });
    }

It fails on execute call. The app enters the catch section, but there's no understandable description in e. And of course, my mobile phone is connected to the same wlan.

Stack trace:

 I/System.out: port:80
 W/System.err: android.os.NetworkOnMainThreadException
 W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
 W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389)
 W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
 W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
 W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
 W/System.err:     at java.net.Socket.connect(Socket.java:631)
 W/System.err:     at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.kt:63)
 W/System.err:     at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
 W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207)
 W/System.err:     at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
 W/System.err:     at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
 W/System.err:     at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
 W/System.err:     at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
 W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
 W/System.err:     at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
 W/System.err:     at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
 W/System.err:     at com.example.donotdisturb.FirstFragment$1.onClick(FirstFragment.java:52)
 W/System.err:     at android.view.View.performClick(View.java:6603)
 W/System.err:     at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
 W/System.err:     at android.view.View.performClickInternal(View.java:6576)
 W/System.err:     at android.view.View.access$3100(View.java:780)
 W/System.err:     at android.view.View$PerformClick.run(View.java:26088)
 W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
 W/System.err:     at android.os.Looper.loop(Looper.java:193)
 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6705)
 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
 W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
MikkoR
  • 41
  • 1
  • 8

1 Answers1

0

I'm not sure if you were able to solve this already, but from the stack trace it looks like you shouldn't be making an API call from the main thread.

I'm more for familiar with using Kotlin on Android and would normally suggest using a coroutine here, but it seems like the answers to this question might help you out when using Java.

If I'm off the mark here, I'd be interested in hearing how you solved this.