公共类MainActivity扩展了AppCompatActivity {
我在哪里设置了url和HttpUrlConnection,允许程序读取网址的内容。
public class DownloadTask 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(Exception e){
e.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask task = new DownloadTask();
String result = null;
try {
result = task.execute("http://www.historyplace.com/specials/").get();
Log.i("Contents of URL", result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
我相信我的代码写得正确,因为logCat会显示几行代码。不幸的是它没有显示所有内容。我试图首先检索所有代码,以便我可以使用正则表达式为教育学校项目提取某些图片和名称。