我正在尝试在我的Android应用中获取HTML网站的源代码 我使用了两种不同的方法来做到这一点:
使用扩展AsyncTask的新类,构建URLconnecting和InputStream并按char复制源代码char:
String result = "";
URL url;
HttpURLConnection httpURLConnection = null;
try {
url = new URL(urls[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int data = inputStreamReader.read();
while (data != -1){
char current = (char) data;
result += current;
data = inputStreamReader.read();
}
return result;
} catch (IOException e) {
e.printStackTrace();
return "Failed";
}
}
我使用的更有效的方法是使用离子库,该离子库仅用较少的代码即可完成基本相同的操作。
Ion.with(getApplicationContext())。load(“ http://www.posh24.se/kandisar”)。asString()
我遇到的问题是,我得到的源代码实际上不是源代码,而是打开页面时在Windows上按F12时看到的“元素”选项卡。 我想获取一个网站的实际源代码,即通过右键单击任意位置获得的网站-查看源代码。 为什么我没有得到源代码本身的任何想法?