So after 3 days of wanting to jump off a cliff. I found the answer. Of course it was a few minutes after asking the question here (first question ever so be kind.). The only Issue is you need a SSL cert for HTTPS on the server retrieving the file. My server is http but i can get a cert in there and fix that. to Test i threw up a github repository and linked to the raw text file. Here is my solution if this saves you 3 days pour one out for me.
Thread {
try {
val url = URL("https://raw.githubusercontent.com/USERNAME/NeedHTTPSdontWanaSSL/main/info.txt")
val uc: HttpsURLConnection = url.openConnection() as HttpsURLConnection
val br = BufferedReader(InputStreamReader(uc.getInputStream()))
var line: String?
val lin2 = StringBuilder()
while (br.readLine().also { line = it } != null) {
lin2.append(line)
}
Log.d("The Text", "$lin2")
} catch (e: IOException) {
Log.d("texts", "onClick: " + e.getLocalizedMessage())
e.printStackTrace()
}
}.start()
Credit: answered Aug 12, 2018 at 9:29 Aishik kirtaniya Android - How can I read a text file from a url?
Answer from GraduatedImposterSynd on Stack OverflowSo after 3 days of wanting to jump off a cliff. I found the answer. Of course it was a few minutes after asking the question here (first question ever so be kind.). The only Issue is you need a SSL cert for HTTPS on the server retrieving the file. My server is http but i can get a cert in there and fix that. to Test i threw up a github repository and linked to the raw text file. Here is my solution if this saves you 3 days pour one out for me.
Thread {
try {
val url = URL("https://raw.githubusercontent.com/USERNAME/NeedHTTPSdontWanaSSL/main/info.txt")
val uc: HttpsURLConnection = url.openConnection() as HttpsURLConnection
val br = BufferedReader(InputStreamReader(uc.getInputStream()))
var line: String?
val lin2 = StringBuilder()
while (br.readLine().also { line = it } != null) {
lin2.append(line)
}
Log.d("The Text", "$lin2")
} catch (e: IOException) {
Log.d("texts", "onClick: " + e.getLocalizedMessage())
e.printStackTrace()
}
}.start()
Credit: answered Aug 12, 2018 at 9:29 Aishik kirtaniya Android - How can I read a text file from a url?
How about this?
import java.net.URL
val s = "https://www.someplace.com/dir/file.txt"
val text = URL(s).openStream().readAllBytes().decodeToString()
Wrap it in some exception handling if you want. So long as you don't keep references to the intermediate stream, it'll immediately be eligible for garbage collection. The stream will be closed when it's garbage collected, thus I don't bother with adding a call to explicitly close it.
Can I convert url to File?
java - Android - How can I read a text file from a url? - Stack Overflow
How to read a text file from resources in Kotlin? - Stack Overflow
Load image file in Kotlin
Videos
Hi everyone, i'm making tg bot using JAICF framework. I want bot to send video, but this command requires File class (prt sc down below), but I have only url (String) from youtube. Any ideas how can I convert this?
Try using an HTTPUrlConnection or a OKHTTP Request to get the info, here try this:
Always do any kind of networking in a background thread else android will throw a NetworkOnMainThread Exception
new Thread(new Runnable(){
public void run(){
ArrayList<String> urls=new ArrayList<String>(); //to read each line
//TextView t; //to show the result, please declare and find it inside onCreate()
try {
// Create a URL for the desired page
URL url = new URL("http://somevaliddomain.com/somevalidfile"); //My text file location
//First open the connection
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(60000); // timing out in a minute
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//t=(TextView)findViewById(R.id.TextView1); // ideally do this in onCreate()
String str;
while ((str = in.readLine()) != null) {
urls.add(str);
}
in.close();
} catch (Exception e) {
Log.d("MyTag",e.toString());
}
//since we are in background thread, to post results we have to go back to ui thread. do the following for that
Activity.this.runOnUiThread(new Runnable(){
public void run(){
t.setText(urls.get(0)); // My TextFile has 3 lines
}
});
}
}).start();
1-) Add internet permission to your Manifest file.
2-) Make sure that you are launching your code in separate thread.
Here is the snippet which works for me great.
public List<String> getTextFromWeb(String urlString)
{
URLConnection feedUrl;
List<String> placeAddress = new ArrayList<>();
try
{
feedUrl = new URL(urlString).openConnection();
InputStream is = feedUrl.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) // read line by line
{
placeAddress.add(line); // add line to list
}
is.close(); // close input stream
return placeAddress; // return whatever you need
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
Our reader function is ready, let's call it by using another thread
new Thread(new Runnable()
{
public void run()
{
final List<String> addressList = getTextFromWeb("http://www.google.com/sometext.txt"); // format your URL
runOnUiThread(new Runnable()
{
@Override
public void run()
{
//update ui
}
});
}
}).start();
val fileContent = MySpec::class.java.getResource("/html/file.html").readText()
No idea why this is so hard, but the simplest way I've found (without having to refer to a particular class) is:
fun getResourceAsText(path: String): String? =
object {}.javaClass.getResource(path)?.readText()
It returns null if no resource with this name is found (as documented).
And then passing in an absolute URL, e.g.
val html = getResourceAsText("/www/index.html")!!