Вывести содержимое главной страницы введенной ссылки
Есть задание определить, существует ли сайт по заданному адресу, если есть - вывести содержание главной страницы. Использовать явно метод GET. Вот как я выводил размер, как сделать так же только получив содержание страницы?
val url = URL(urlString)
try {
val httpURLConnection = url.openConnection() as HttpURLConnection
httpURLConnection.requestMethod = "GET"
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0")
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
httpURLConnection.setRequestProperty("Content-Language", "en-US")
httpURLConnection.setRequestProperty("Accept", "application/json")
if (httpURLConnection.responseCode == 200) {
it.onSuccess("Size is ${httpURLConnection.contentLenght}")
} else {
it.onError(Throwable("Website is not available"))
}
Ответы (1 шт):
Автор решения: Andrew
→ Ссылка
Получить сайт можно при помощи такого способа:
private class MyTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String ... urls)
{
try
{
URL url = new URL(urls[0]);
URLConnection uc = url.openConnection();
//String j = (String) uc.getContent();
uc.setDoInput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
catch (Exception e)
{
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(getApplication(), result, Toast.LENGTH_LONG).show();
}
}
и получение при помощи класса:
MyTask taskLoad = new MyTask();
taskLoad.execute("http://10.0.0.8/mybiren/JSON.php?day=Monday");
Вот так же есть подобный вопрос.