Я изменяю свое сообщение из-за многочисленных изменений, которые я сделал. Он все еще не запускается после нажатия на кнопку, но теперь я получаю другую ошибку.
код
package com.example.dsfrs_soup_news
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import org.jsoup.Jsoup
import java.io.IOException
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//java.net.URL url = new java.net.URL("http//www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35")
try{
// get reference to button
val BtnGo = findViewById<Button>(R.id.BtnGo)
val TxtResults: TextView = findViewById<TextView>(R.id.TxtResults)
// set on-click listener
BtnGo.setOnClickListener {
Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
val url = java.net.URLEncoder.encode("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35", "UTF-8")
val doc = Jsoup.connect(url).get()
val links = doc.select("tr td:first-of-type a")
.map { col -> col.attr("href") } //href gets each link
//for every link display the gumf, ................consider just showing todays, exit loop once total number of incidents has been displayed
links.forEach { links ->
//println("http://www.dsfire.gov.uk/News/Newsdesk/$links \n")
TxtResults.append("http://www.dsfire.gov.uk/News/Newsdesk/$links \n")
// work with each link, get details from incident page
val sIncident = Jsoup.connect("http://www.dsfire.gov.uk/News/Newsdesk/$links").get()
var aLLDeetz = sIncident.select("td").toString() //convert to string to allow tidying
//got all the details of the shout, now replace useless charecters
aLLDeetz = aLLDeetz.replace("<td>", "")
aLLDeetz = aLLDeetz.replace("</td>", "")
aLLDeetz = aLLDeetz.replace("<td width=\"259\">", "")
aLLDeetz = aLLDeetz.replace(":\\n", ": ")
aLLDeetz = aLLDeetz.trim()
//display results
TxtResults.append("Icident Details are: \n $aLLDeetz")
//println(message = "Icident Details are: \n $aLLDeetz")
//get all information in the incident summary and convert to string for cleaning
var IncSumm = sIncident.getElementById("IncidentDetailContainer").toString()
//clean useless chars out of string
IncSumm = IncSumm.replace("<p>", "")
IncSumm = IncSumm.replace("</div>", "")
IncSumm = IncSumm.replace("<br></p>", "")
IncSumm = IncSumm.replace("<br>", "")
IncSumm = IncSumm.replace("<div id=\"IncidentDetailContainer\">", "")
IncSumm = IncSumm.replace(" </p>", "")
IncSumm = IncSumm.replace("</p>", " ")
IncSumm = IncSumm.replace("<p align=\"justify\">", "")
IncSumm = IncSumm.replace(" ", "")
//display results
//println(IncSumm)
TxtResults.append(IncSumm)
finish() // other posts have put this in to stop forced close
}
}
} catch (e: IOException) {
Toast.makeText(this@MainActivity, "Error is $e.", Toast.LENGTH_SHORT).show()
}//try bracket
}
}
ошибка
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dsfrs_soup_news, PID: 22997
java.lang.IllegalArgumentException: Malformed URL: http%3A%2F%2Fwww.dsfire.gov.uk%2FNews%2FNewsdesk%2FIncidentsPast7days.cfm%3FsiteCategoryId%3D3%26T1ID%3D26%26T2ID%3D35
at org.jsoup.helper.HttpConnection.url(HttpConnection.java:131)
at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:70)
at org.jsoup.Jsoup.connect(Jsoup.java:73)
at com.example.dsfrs_soup_news.MainActivity$onCreate$1.onClick(MainActivity.kt:28)
at android.view.View.performClick(View.java:7189)
at android.view.View.performClickInternal(View.java:7163)
at android.view.View.access$3500(View.java:821)
at android.view.View$PerformClick.run(View.java:27579)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
Caused by: java.net.MalformedURLException: no protocol: http%3A%2F%2Fwww.dsfire.gov.uk%2FNews%2FNewsdesk%2FIncidentsPast7days.cfm%3FsiteCategoryId%3D3%26T1ID%3D26%26T2ID%3D35
at java.net.URL.<init>(URL.java:601)
at java.net.URL.<init>(URL.java:498)
at java.net.URL.<init>(URL.java:447)
at org.jsoup.helper.HttpConnection.url(HttpConnection.java:129)
at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:70)
at org.jsoup.Jsoup.connect(Jsoup.java:73)
at com.example.dsfrs_soup_news.MainActivity$onCreate$1.onClick(MainActivity.kt:28)
at android.view.View.performClick(View.java:7189)
at android.view.View.performClickInternal(View.java:7163)
at android.view.View.access$3500(View.java:821)
at android.view.View$PerformClick.run(View.java:27579)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
Я не уверен, как получить URL-адрес ... НЕ быть уродливым ха-ха. У меня это было прямо в выражении .get "http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35", но это тоже дало. После поиска я нашел эту новую кодировку URL-адреса (UTF-8)
Пожалуйста ... это действительно важно для меня, ха-ха
Любая помощь будет отличной.