码迷,mamicode.com
首页 > 其他好文 > 详细

异步任务判断服务器是否开启

时间:2015-01-29 12:09:36      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

  
/** 异步任务 :判断服务器开启才取后台数据
  * 2014-7-31
  * @author 吴春源
  *
  */
 public class MyTask extends AsyncTask<String, Integer, String> {               
     //onPreExecute方法用于在执行后台任务前做一些UI操作                          
     @Override                                                                   
     protected void onPreExecute() {                                             
         Log.i(TAG, "onPreExecute() called"); 
        // MyUtil.showToast(OrderActivity.this, "数据上传...");
        // textView.setText("loading..."); 
       //  button_toOrder.setText("正在查询....");
       //  pd = ProgressDialog.show(MenuActivity.this, "查台", "正在连接服务器...", true, true);
   
     }                                                                           
                                                                                 
     //doInBackground方法内部执行后台任务,不可在此方法内修改UI                   
     @Override                                                                   
     protected String doInBackground(String... params) {
      //totalText = (TextView) findViewById(R.id.total);
         Log.i(TAG, "doInBackground(Params... params) called");                  
      
         try{
    Socket socket2  =new Socket();
    //socket2.connect(new  InetSocketAddress(SERVER_ADDRESS, SERVER_PORT));
    //设置连接超时 10s
    socket2.connect(new InetSocketAddress(ServerIP, SERVER_PORT),10000);
     socket2.close();
    
    return  String.valueOf(1)+"|"+params[0];
    
          } catch (Exception e) {                                                 
             Log.e(TAG, e.getMessage());  
             return  String.valueOf(0);
          } 
                                                           
     }                                                                      
                                                                            
     //onProgressUpdate方法用于更新进度信息                                 
     @Override                                                              
     protected void onProgressUpdate(Integer... progresses) {               
         Log.i(TAG, "onProgressUpdate(Progress... progresses) called");     
   
     }                                                                      
                                                                            
     //onPostExecute方法用于在执行完后台任务后更新UI,根据后台下单结果重新绑定前台订单               
    // String result 来自doInBackground
     @Override                                                              
     protected void onPostExecute(String result) {                          
         Log.i(TAG, "onPostExecute(Result result) called");
         String [] sa =result.split("\\|");
          //pd.dismiss();
             if(sa[0].equals("1")){
              /** MyUtil.showToast(MenuActivity.this, "服务器开启"); 
              * */
               if(sa[1].equals("0")){ // 0-取台号
                 // mTTask= new  TableListsTask();                                               
                // mTTask.execute("0");
             TableListsTask task = new TableListsTask(ServerIP,"ALL", MenuActivity.this);
                  //DownLoaderTask task = new DownLoaderTask("http://192.168.9.155/johnny/test.h264", getCacheDir().getAbsolutePath()+"/", this);
                  task.execute();
                  //取订单明细
                  OrderDetailTaskM task1 = new OrderDetailTaskM(ServerIP,"0", MenuActivity.this);
                  task1.execute();
                  
               }
              
               if(sa[1].equals("2")){ // 2-取台号 -某一台
                  // mTTask= new  TableListsTask();                                               
                 // mTTask.execute("0");
              TableListsTask task = new TableListsTask(ServerIP,table_no, MenuActivity.this);
                   //DownLoaderTask task = new DownLoaderTask("http://192.168.9.155/johnny/test.h264", getCacheDir().getAbsolutePath()+"/", this);
                   task.execute();
                  //2014-11-9增加查台取订单明细 
                   OrderDetailTaskM task1 = new OrderDetailTaskM(ServerIP,"0", MenuActivity.this);
                   task1.execute();
                    }
            
              
           
                if(sa[1].equals("1")){ // 1-取新菜单
                 MenuListsTaskM task = new MenuListsTaskM(ServerIP,"0", MenuActivity.this);
                  task.execute();
                     }
         
               }
           if(result.equals("0")){
         MyUtil.showToast(MenuActivity.this, "无法连接服务器"); 
               }
          }                                                                      
                                                                         
     //onCancelled方法用于在取消执行中的任务时更改UI                        
     @Override                                                              
     protected void onCancelled() {                                         
         Log.i(TAG, "onCancelled() called");                                
      } 
 }
 

 

异步任务判断服务器是否开启

标签:

原文地址:http://www.cnblogs.com/realhope/p/4258986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!