How to get html code through a url in android studio

1

I would like to do as I get an html code from a website using android studio. This is the code I made, but it will not return anything.

private static String pegarURL(String a) {
    StringBuilder b = new StringBuilder();

    try {
        URL url = new URL(a);

        URLConnection urlConnection = url.openConnection();

        BufferedReader bufferedReader =
                new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

        String line;

        while ((line = bufferedReader.readLine()) != null) {
            b.append(line + "\n");
        }

        bufferedReader.close();
    } catch(Exception e) {

        e.printStackTrace();
    }

    return b.toString();

}

String h = pegarURL("https://www.google.com/");
    
asked by anonymous 22.09.2018 / 19:47

1 answer

1

The most practical way I've found of doing this is this. Add in the dependencies of your gradle the code below:

implementation 'com.koushikdutta.ion:ion:2.1.8'

From sync, then import the two libraries below

import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

And use the following code to get your HTML code

Ion.with(getApplicationContext()).load("http://www.google.com").asString().setCallback(new FutureCallback<String>() { 
            @Override
            public void onCompleted(Exception e, String result) {
                Log.i("RESULTADO",result); // Aqui você trabalha com a resposta
            }
        });
    
23.09.2018 / 15:42