中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android AsyncTask異步處理抓取網(wǎng)頁

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

Android AsyncTask異步處理抓取網(wǎng)頁

/**
 *
 * @author yanggang
 * @see http://blog.csdn.net/sunboy_2050
 */
public class MainActivity extends Activity {
    private EditText metURL;
    private TextView mtvPage;
    private Button mbtnConn;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        metURL = (EditText)findViewById(R.id.etURL);        // 輸入網(wǎng)址
        mbtnConn = (Button)findViewById(R.id.btnConn);      // 連接網(wǎng)站
        mtvPage = (TextView)findViewById(R.id.tvPage);      // 顯示網(wǎng)頁
         
        mbtnConn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                connURL();
            }
        });
    }
 
    private void connURL(){
        URLTask urlTask = new URLTask(this);    // 實例化抽象AsyncTask
        urlTask.execute(metURL.getText().toString().trim());    // 調(diào)用AsyncTask,傳入url參數(shù)
    }
     
    /** 繼承AsyncTask的子類,下載url網(wǎng)頁內(nèi)容 */
    class URLTask extends AsyncTask<String, Integer, String> {
        ProgressDialog proDialog;
         
        public URLTask(Context context) {
            proDialog = new ProgressDialog(context, 0);
            proDialog.setButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            proDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    finish();
                }
            });
            proDialog.setCancelable(true);
            proDialog.setMax(100);
            proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            proDialog.show();
        }
 
        @Override
        protected void onPreExecute(){
            mtvPage.setText(R.string.hello_world);      // 可以與UI控件交互
        }
         
        @Override
        protected String doInBackground(String... params) {     // 在后臺,下載url網(wǎng)頁內(nèi)容
            try {
                HttpGet get = new HttpGet(params[0]);           // url
                HttpResponse response = new DefaultHttpClient().execute(get);
                 
                if(response.getStatusLine().getStatusCode() == 200) {       // 判斷網(wǎng)絡連接是否成功
//                  String result = EntityUtils.toString(response.getEntity(), "gb2312");   // 獲取網(wǎng)頁內(nèi)容
//                  return result;
                     
                    HttpEntity entity = response.getEntity();
                    long len = entity.getContentLength();       // 獲取url網(wǎng)頁內(nèi)容總大小
                    InputStream is = entity.getContent();
                     
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int ch = -1;
                    int count = 0;      // 統(tǒng)計已下載的url網(wǎng)頁內(nèi)容大小
                    while(is != null && (ch = is.read(buffer)) != -1 ) {
                        bos.write(buffer, 0, ch);
                        count += ch;
                        if(len > 0) {
                            float ratio = count/(float)len * 100;   // 計算下載url網(wǎng)頁內(nèi)容百分比
                            publishProgress((int)ratio);    // android.os.AsyncTask.publishProgress(Integer... values)
                        }
                        Thread.sleep(100);
                    }
                    String result = new String(bos.toString("gb2312"));
                    return result;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
             
            return null;
        }
         
        @Override
        protected void onProgressUpdate(Integer... values) {    // 可以與UI控件交互
            mtvPage.setText("" + values[0]);    // 獲取 publishProgress((int)ratio)的values
            proDialog.setProgress(values[0]);
        }
         
        @Override
        protected void onPostExecute(String result) {   // 可以與UI控件交互
            mtvPage.setText(result);
            proDialog.dismiss();
        }
    }
}

標簽: ssd 網(wǎng)絡

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇: iOS獲取視圖控制器實例的方法

下一篇:java正則表達式判斷郵件地址是否合法