首页 > 代码库 > Android 【山西违章查询实现2】(解析网页源码然后ListView显示出来)--- 客户端获取html源码

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); //获得源码 } }

获取结果

技术分享

Android 【山西违章查询实现2】(解析网页源码然后ListView显示出来)--- 客户端获取html源码