1

Whenever this code is executed, the app crashes and no data is returned.

I have already tried Volley and okhttp and neither worked.

val url = "https://..." 

val lines = URL(url).openStream().use {
    it.bufferedReader().readLines()        
}

outputText.text = lines.toString()

I expect to see the returned information but instead the app crashes.

Divins Mathew
  • 2,214
  • 3
  • 20
  • 31
cdonn9
  • 13
  • 4

1 Answers1

1

Without your error log, I can only assume that you are getting the NetworkOnMainThreadException Exception.

If that's the case, You should know that in android you can't run network related actions on the main thread - you need to run them on a different thread, for example:

Thread mThread = new Thread(new Runnable() {

@Override
public void run() {
    try  {
    //Put your code that you want to run in here.
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
});

mThread.start
Tamir Abutbul
  • 6,472
  • 7
  • 16
  • 44