android 設定網路請求逾時

來源:互聯網
上載者:User

標籤:android   des   http   os   io   for   art   ar   

UI介面更新必須在ui線程中 不能在ruanable線程中操作ui 可以發送訊息利用handler來更新ui
    private void load() {
        LoadDate load = new LoadDate();
        load.execute("http://h.hiphotos.baidu.com/image/w%3D310/sign=1bc9c0da38292df597c3aa148c305ce2/c83d70cf3bc79f3d77fbe1c5b8a1cd11728b2928.jpg");
    }

    class LoadDate extends AsyncTask<String, Integer, Bitmap> {

        @Override
        protected void onPreExecute() {

//            myDialog.setTitle("請稍候");
//            myDialog.setMessage("loading......");
//            myDialog.setCanceledOnTouchOutside(false);
//            myDialog.show();
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            URL myFileUrl = null;
            Bitmap bitmap = null;
            InputStream is = null;
            HttpURLConnection conn = null;
            
            
             try {
                    myFileUrl = new URL(params[0]);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    conn = (HttpURLConnection)myFileUrl
                            .openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    conn.setConnectTimeout(3000);
                    conn.setReadTimeout(3000);
                    is =conn.getInputStream();
                    bitmap =BitmapFactory.decodeStream(is);
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{                
                        try {
                            if(is != null){
                            is.close();
                            }
                            if( conn != null){
                                conn.disconnect();
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }                
                }
                startTime(bitmap);
                return bitmap;
                
        }

        Handler myHandler=new Handler(){
            public void handleMessage(Message msg) {
                if(msg.what==1){
                    Toast.makeText(MainActivity.this, "串連伺服器逾時"+msg.obj, 1).show();
                }
            };
        };
        private void startTime( final Bitmap bm) {
            Runnable run=new Runnable() {
                boolean isRun=true;
                long enableTime;
                long startTime=System.currentTimeMillis();
                @Override
                public void run() {
                    System.out.println(bm+"---bm----");
                    while(isRun){
                        enableTime=System.currentTimeMillis()-startTime;
                        if(enableTime>=3000 &&bm==null){
                            Message message = new Message();  
                            message.what = 1;  
                            message.obj="demo";
                            myHandler.sendMessage(message);  
//                            Toast.makeText(MainActivity.this, "串連伺服器逾時", 1).show();
                            isRun=false;
                        }
                         try{  
                                Thread.sleep(50);  
                            }catch (Exception e) {  
                                System.out.println("計時器線程 sleep ex:"+e.toString());  
                            }  
                        }  
                        System.out.println("計時器線程run..end time:"+enableTime);  
                    }  
                };  
//                Looper.prepare();
                  
                new Thread(run).start();  
            
        }

        @Override
        protected void onPostExecute(Bitmap result) {
             image.setImageBitmap(result);        
//            myDialog.cancel();
             System.out.println(result+"---");
        }
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.