[Java] Android AsyncTask异步处理抓取网页 →→→→→进入此内容的聊天室

来自 , 2021-02-13, 写在 Java, 查看 101 次.
URL http://www.code666.cn/view/21e4ef94
  1. /**
  2.  *
  3.  * @author yanggang
  4.  * @see http://blog.csdn.net/sunboy_2050
  5.  */
  6. public class MainActivity extends Activity {
  7.         private EditText metURL;
  8.         private TextView mtvPage;
  9.         private Button mbtnConn;
  10.  
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.  
  16.         metURL = (EditText)findViewById(R.id.etURL);            // 输入网址
  17.         mbtnConn = (Button)findViewById(R.id.btnConn);          // 连接网站
  18.         mtvPage = (TextView)findViewById(R.id.tvPage);          // 显示网页
  19.        
  20.         mbtnConn.setOnClickListener(new View.OnClickListener() {
  21.                         @Override
  22.                         public void onClick(View v) {
  23.                                 connURL();
  24.                         }
  25.                 });
  26.     }
  27.  
  28.     private void connURL(){
  29.         URLTask urlTask = new URLTask(this);    // 实例化抽象AsyncTask
  30.         urlTask.execute(metURL.getText().toString().trim());    // 调用AsyncTask,传入url参数
  31.     }
  32.    
  33.     /** 继承AsyncTask的子类,下载url网页内容 */
  34.     class URLTask extends AsyncTask<String, Integer, String> {
  35.         ProgressDialog proDialog;
  36.        
  37.         public URLTask(Context context) {
  38.                 proDialog = new ProgressDialog(context, 0);
  39.                 proDialog.setButton("cancel", new DialogInterface.OnClickListener() {
  40.                                 @Override
  41.                                 public void onClick(DialogInterface dialog, int which) {
  42.                                         dialog.cancel();
  43.                                 }
  44.                         });
  45.                 proDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
  46.                                 @Override
  47.                                 public void onCancel(DialogInterface dialog) {
  48.                                         finish();
  49.                                 }
  50.                         });
  51.                 proDialog.setCancelable(true);
  52.                 proDialog.setMax(100);
  53.                 proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  54.                 proDialog.show();
  55.         }
  56.  
  57.         @Override
  58.         protected void onPreExecute(){
  59.                 mtvPage.setText(R.string.hello_world);          // 可以与UI控件交互
  60.         }
  61.        
  62.                 @Override
  63.                 protected String doInBackground(String... params) {             // 在后台,下载url网页内容
  64.                         try {
  65.                                 HttpGet get = new HttpGet(params[0]);                   // url
  66.                                 HttpResponse response = new DefaultHttpClient().execute(get);
  67.                                
  68.                                 if(response.getStatusLine().getStatusCode() == 200) {           // 判断网络连接是否成功
  69. //                                      String result = EntityUtils.toString(response.getEntity(), "gb2312");   // 获取网页内容
  70. //                                      return result;
  71.                                        
  72.                                         HttpEntity entity = response.getEntity();
  73.                                         long len = entity.getContentLength();           // 获取url网页内容总大小
  74.                                         InputStream is = entity.getContent();
  75.                                        
  76.                                         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  77.                                         byte[] buffer = new byte[1024];
  78.                                         int ch = -1;
  79.                                         int count = 0;          // 统计已下载的url网页内容大小
  80.                                         while(is != null && (ch = is.read(buffer)) != -1 ) {
  81.                                                 bos.write(buffer, 0, ch);
  82.                                                 count += ch;
  83.                                                 if(len > 0) {
  84.                                                         float ratio = count/(float)len * 100;   // 计算下载url网页内容百分比
  85.                                                         publishProgress((int)ratio);    // android.os.AsyncTask.publishProgress(Integer... values)
  86.                                                 }
  87.                                                 Thread.sleep(100);
  88.                                         }
  89.                                         String result = new String(bos.toString("gb2312"));
  90.                                         return result;
  91.                                 }
  92.                         } catch (Exception e) {
  93.                                 e.printStackTrace();
  94.                         }
  95.                        
  96.                         return null;
  97.                 }
  98.                
  99.                 @Override
  100.                 protected void onProgressUpdate(Integer... values) {    // 可以与UI控件交互
  101.                         mtvPage.setText("" + values[0]);        // 获取 publishProgress((int)ratio)的values
  102.                         proDialog.setProgress(values[0]);
  103.                 }
  104.                
  105.                 @Override
  106.                 protected void onPostExecute(String result) {   // 可以与UI控件交互
  107.                         mtvPage.setText(result);
  108.                         proDialog.dismiss();
  109.                 }
  110.     }
  111. }
  112. //java/5540

回复 "Android AsyncTask异步处理抓取网页"

这儿你可以回复上面这条便签

captcha