2012-08-28 62 views
0

onCreate()代碼開始TimerTask的Android run()方法不執行

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
Log.e(LOG_TAG, "Start Repeat Timer"); 
    TimerTask task = new RepeatingTask(); 
    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(task, 0, 3000); 
    Log.e(LOG_TAG, "Started Repeat Timer"); 
} 

計時器任務代碼:

public class RepeatingTask extends TimerTask { 
    //private int len = 0; 
    //private byte[] input = new byte[len]; 

    public RepeatingTask() { 
      Log.e(LOG_TAG, "In RepeatingTask()"); 
      Log.e(LOG_TAG, "Before inputJSON String"); 

      String hello = "hello world"; 
      //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream())); 
      try { 
        inputJSON = ConvertByteArrayToString(readBytes(inputStr)); 
        sendBytes(ConvertStringToByteArray(inputJSON), 0, ConvertStringToByteArray(inputJSON).length); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      //Convert 
      Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 


     //LOOK HERE FIRST 
     //inputJSON is what is received back from the server - Take the inputJSON 
     //String and use regular expressions HERE to remove all the other characters in the 
     //string except the payload JSON. 
     //refreshViewModels(inputJSON); 
    } 

    @Override 
    public void run() { 
      /*try { 
       Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON"); 
       //outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK 
       //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr)); 
       inputJSON = ConvertByteArrayToString(getFileBytes(inputStr)); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON); 
      refreshViewModels(inputJSON);*/ 

     try { 
      Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON"); 
      //outputstrwr.write(outputJSONserv); //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK 
      //byte[] = myByteArray = readBytes(inputStr); 
      sendBytes(ConvertStringToByteArray(outputJSONserv), 0, ConvertStringToByteArray(outputJSONserv).length); 
      //sendBytes(myByteArray, 0, myByteArray.length); 
      Log.e(LOG_TAG, "AFTER SENDING DATA"); 
      //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr)); 
        inputJSON = ConvertByteArrayToString(readBytes(inputStr)); 
        Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON); 
      } 
     catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 

     Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON3:" + inputJSON); 
     refreshViewModels(inputJSON); 
    } 
} 

inputStr是用於讀取字節並將它們轉換的InputStream成一個字符串。

ConvertByteArrayToString()剛轉換的字節數組String & ConvertStringToByteArray()轉換一個StringByte[],使得數據可以使用sendBytes(byte[], int, int)發送。

我試圖找出爲什麼我的代碼卡在這個try/catch語句沒有任何異常被拋出:

try { 
        inputJSON = ConvertByteArrayToString(readBytes(inputStr)); 
        sendBytes(ConvertStringToByteArray(inputJSON), 0, ConvertStringToByteArray(inputJSON).length); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

這裏是我的sendBytes()方法:

public void sendBytes(byte[] myByteArray, int start, int len) throws IOException { 
     if (len < 0) 
      throw new IllegalArgumentException("Negative length not allowed"); 
     if (start < 0 || start >= myByteArray.length) 
      throw new IndexOutOfBoundsException("Out of bounds: " + start); 
     // Other checks if needed. 

     // May be better to save the streams in the support class; 
     // just like the socket variable. 
     OutputStream out = socket.getOutputStream(); 
     DataOutputStream dos = new DataOutputStream(out); 

     dos.writeInt(len); 
     if (len > 0) { 
      dos.write(myByteArray, start, len); 
     } 
    } 

這裏getPayloadStr()方法:

public String getPayloadStr(String profileString) { 
     Log.e("LOG_TAG", "Profile Str:"+profileString); 
     Pattern pattern = Pattern.compile(".*?payload\":(.*)\\}"); 

     Log.e("LOG_TAG", "I got here 1"); 
     Matcher matcher = pattern.matcher(profileString); 
     Log.e("LOG_TAG", "I got here 12"); 
     //Matcher m = responseCodePattern.matcher(firstHeader); 
     matcher.matches(); 
     matcher.groupCount(); 
     //matcher.group(0); 
     Log.e("LOG_TAG", "I got here 2"+matcher.group(1)); 
     return matcher.group(1); 
    } 

這是我的readBytes()方法:

public byte[] readBytes(InputStream in) throws IOException { 
     // Again, probably better to store these objects references in the support class 
     in = socket.getInputStream(); 
     DataInputStream dis = new DataInputStream(in); 

     int len = dis.readInt(); 
     byte[] data = new byte[len]; 
     if (len > 0) { 
      dis.readFully(data); 
     } 
     return data; 
    } 

任何幫助或指針在正確的方向將不勝感激。任何通過聊天的幫助也將不勝感激。如果需要,整個文件可以發佈或在聊天會話中查看。

+0

我願意把我的眼睛''dis.readFully(數據)''在''readBytes''法第一。在之前和之後添加日誌輸出。 – harism

回答

0

我的想法是:

  • 離開它emprty的conrructor和設置斷點運行()-hopefully hitted,否則你會在日誌中看到的IN REPEATINGTHREAD-INPUTJSON消息。如果不是,我不知道問題在哪裏,因爲代碼似乎沒問題。
  • ,如果你看到的,比在構造器的問題(問題1隔離)

我覺得這裏的問題是:

INT LEN = dis.readInt();

讀取它是一個阻塞方法,所以你的代碼在Even調度程序線程或主線程或Ui線程中 - 忘記了那個平臺上的名稱 - 正在等待,直到他可以讀取4個字節。

基於註釋和代碼的請求,那就是:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Log.e(LOG_TAG, "Start Repeat Timer"); 

     Thread thInitializer = new Thread() { 
      @Override 
      public void run() { 
       TimerTask task = new RepeatingTask(); // here will block until is readed from socket, but will not block the UI 

       //after read is done you can set breakpoint to next statement, but it will repet the run method of the RepeatingTask in each 3 sec 
       Timer timer = new Timer(); 
       timer.scheduleAtFixedRate(task, 0, 3000); 
       Log.e(LOG_TAG, "Started Repeat Timer"); 
      } 
     }; 
     thInitializer.start(); 
     // UI initialization is done, background thread is running an trying to initialize the network stuff   
    } 
+0

所以你會推薦我用readInt()來做什麼? – user268397

+0

不,你必須改變邏輯,並在後臺線程中執行該代碼 – 2012-08-29 00:23:54

+0

例如在構造函數中添加一個新的線程創建代碼,並且該線程將讀取並在他的run()方法中傳入字節 – 2012-08-29 00:30:52