Search code examples
javaandroidinputstreamhttpurlconnectionioexception

Android internet access throws IOException


I am trying to write an app that pings a web page, which will then trigger an action.

package com.anton.lightcontrol;

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Switch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {

    Switch onOffSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onOffSwitch = (Switch) findViewById(R.id.onOffSwitch);
        onOffSwitch.setChecked(true);
        onOffSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onOffSwitch.isChecked()==true) {
                    onOffSwitch.setTextColor(Color.GREEN);
                    LightConnection("http://raspberrypi/toggle_on.py");



                }
                else if (onOffSwitch.isChecked()==false){
                    onOffSwitch.setTextColor(Color.RED);
                    LightConnection("http://raspberrypi/toggle_off.py");

                }
            }
        });
    }

    private void LightConnection(final String accessUrl){
        (new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                Log.d("EXEC", "#1");
                URL url = new URL(accessUrl);
                Log.d("EXEC", "#2");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                Log.d("EXEC", "#3");

                InputStreamReader input = new InputStreamReader(conn.getInputStream(), "utf-8");
                Log.d("EXEC", "#4");

                BufferedReader reader = new BufferedReader(input,2000);
                Log.d("EXEC", "#5");
                String line = reader.readLine();
                Log.d("EXEC", "#6");


            }
                catch (IOException e){
                    Log.d("EXEC", "#10 - CATCH");
                    onOffSwitch.setTextColor(Color.YELLOW);
                }

            }
        })).start();
    }
}

My debug output looks like the following:

D/EXEC: #1
D/EXEC: #2
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/EXEC: #3
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
D/EXEC: #10 - CATCH

I have been searching for a solution for the isSBSettingEnabled false error, but I couldn't find anything that helped me.

I am running my app on a Samsung M20 smartphone.


Solution

  • This technically isn't an error, since it's alerting you that you're not using a network security config. More information on this here: Network security configuration: Android Developer Docs.

    However, if you're limited to using HTTP, you can resolve connection issues by permitting cleartext traffic by adding this to your <application> tag in AndroidManifext.xml:

    android:usesCleartextTraffic="true"