废话不多说,直接上源码
最后网页源码在result中。
public void btn(View view) { new myWorker().execute("这里是网址"); } //异步操作,防止UI线程阻塞。 private class myWorker extends AsyncTask<String,Void,String> { protected String doInBackground(String...urls) { StringBuilder builder = new StringBuilder(100000); for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse execute = client.execute(httpGet); InputStream content = execute.getEntity().getContent(); BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); String s = ""; while ((s = buffer.readLine()) != null) { builder.append(s); } } catch (Exception e) { e.printStackTrace(); } } return builder.toString(); } protected void onPostExecute(String result) { //源码显示在Textview中 TextView textView=(TextView)findViewById(R.id.tv1); textView.setText(result); //获得源码 } }
获取结果
时间: 2024-10-14 20:00:28