Android 【山西违章查询实现2】(解析网页源码然后ListView显示出来)--- 客户端获取html源码
废话不多说,直接上源码
最后网页源码在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); //获得源码 } }
获取结果
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。