Reputation: 13
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.
Upvotes: 1
Views: 488
Reputation: 7661
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
Upvotes: 1