android多线程读取网页内容
生活随笔
收集整理的這篇文章主要介紹了
android多线程读取网页内容
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
android必須使用子線程才能夠做耗時操作,這點雖然比較符合優秀應用的特點,但是多線程真是讓人有點頭疼,不管怎么樣,那幾個runable,handler什么的,我真心記不住它里面有些什么,所以我寫篇博文,把我以前的android多線程代碼貼上來,等忘掉的時候好來看看。
private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what){case 1:viewPager.setCurrentItem(currentItem);// 切換當前顯示的圖片break;case 2:if(title!=null){ListView newsList = (ListView)findViewById(R.id.homepage_newslist);ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();HashMap<String, String> item;for(int i =0;i<title.length;i++){item=new HashMap<String, String>();item.put("titile", title[i]);item.put("time", time[i]);list.add(item);}SimpleAdapter adapter = new SimpleAdapter(Homepage.this,list,R.layout.homepagelistitem,new String[]{"titile","time"},new int[]{R.id.homepage_newstitle,R.id.homepage_newstime});newsList.setAdapter(adapter);//for(int i =0;i<bitstr.length;i++){titles[i] = bitstr[i];}if(bitUrl!=null){bit = new Bitmap[5];Runnable thread = new Runnable(){@Overridepublic void run(){//這里下載數據try{for(int m=0;m<bitUrl.length;m++){URL url= new URL(bitUrl[m]);HttpURLConnection conn= (HttpURLConnection)url.openConnection();conn.setDoInput(true);conn.connect();InputStream inputStream=conn.getInputStream();bit[m]= BitmapFactory.decodeStream(inputStream);}Message msg=new Message();msg.what=3;handler.sendMessage(msg);}catch(MalformedURLException e1){e1.printStackTrace();}catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace(); }}};new Thread(thread).start();}}break;case 3:if(bit!=null){for(int j=0;j<bit.length;j++){ImageView p_w_picpathView = new ImageView(Homepage.this);p_w_picpathView.setImageBitmap(bit[j]);p_w_picpathView.setScaleType(ScaleType.FIT_XY);p_w_picpathViews.add(msg.arg1, p_w_picpathView);}}else{Toast.makeText(getApplicationContext(), "加載圖片失敗,請檢查網絡",Toast.LENGTH_SHORT).show();}break;}};};以上為主線程的Ui更改代碼,msg.what == 1的時候是一個圖片切換的消息,大家忽略它
msg.what == 2表示從網絡中已經獲取到了文字信息,可以更新了文字顯示了,我將文字放在了一個listview里面,所以用了SimpleAdapter還有數組。。
msg.what == 3表示圖片下載好了,可以裝載圖片了,也是用來更新UI的代碼。。
網頁文字下載函數,同時獲取圖片地址:
Runnable thread = new Runnable(){@Overridepublic void run() {//這里下載數據try{URL url= new URL("http://xx.xx.xx.xx/MobileApp/QueryShopInfo.aspx?userid=EMHHBMDTAw4=");HttpURLConnection conn= (HttpURLConnection)url.openConnection();conn.setDoInput(true);conn.connect();InputStream inputStream=conn.getInputStream();if(inputStream!=null){int length = conn.getContentLength();byte [] buffer = new byte[1024];inputStream.read(buffer);for (int j = 0; j < buffer.length; j++) {if (buffer[j] == 0) {length = j;break;}}byte [] buf=new byte[length];System.arraycopy(buffer, 0, buf, 0, length);String temp1= EncodingUtils.getString(buf, "GB2312");temp1 = temp1.trim();String[] temp2 =temp1.split("&");String[] temp21 = temp2[0].split("#");String[] temp22 = temp2[1].split("#");int i;title = new String[temp22.length];time = new String[temp22.length];;URL =new String[temp22.length];for(i=0;i<temp22.length;i++){String[] temp = temp22[i].split("\\|");title[i] = temp[0];time[i] = temp[1];URL[i] = temp[2];}bitstr = new String[temp21.length];bitUrl = new String[temp21.length];for(i=0;i<temp21.length;i++){String[] temp = temp21[i].split("\\|");bitstr[i] = temp[0];bitUrl[i] = temp[1];}Message msg=new Message();msg.what=2;handler.sendMessage(msg);}}catch(MalformedURLException e1){e1.printStackTrace();}catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}catch (Exception e){e.printStackTrace();}}};new Thread(thread).start(); }下面是圖片讀取函數:
大家可能已經在第一個代碼段中看到了上面這部分代碼,這是因為我要在讀取到網頁文字信息的同時來開辟新的下載線程下載這些圖片,總之就這樣。。。
轉載于:https://blog.51cto.com/shisongran/1203445
總結
以上是生活随笔為你收集整理的android多线程读取网页内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 重要函数实例
- 下一篇: Eclipse常用功能键