Merhaba Logcat içinde bir web sitesi HTML içeriği indirmeye çalışıyorum, böylece gelecekte belirli bilgileri seçebilirim. Ama bunu yapmadan önce, önce web sitesiyle bağlantı kurabilirsem test etmek istiyorum. Şu anda bir hata alıyorum ama bağlamı da alıyorum.Android İndirme HTML içeriği
// public class benim yarattığım
public class DownloadTsk extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
// onCreate yöntem
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTsk task = new DownloadTsk();
String result = null;
try {
result = task.execute("http://www.posh24.com/celebrities").get();
Log.i("Content", result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
// Güncel sonuç
Content: <!DOCTYPE html>
<html>
<head>