Java 线程工作不正常-Android

Java 线程工作不正常-Android,java,android,Java,Android,我的线坏了。它每3秒钟执行RepeatingThread()方法中的所有代码,但不执行run()方法中的所有代码。我做错了什么 代码如下: public RepeatingThread rt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.e(LOG_TAG

我的线坏了。它每3秒钟执行
RepeatingThread()
方法中的所有代码,但不执行
run()
方法中的所有代码。我做错了什么

代码如下:

public RepeatingThread rt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.e(LOG_TAG, "Start Repeat Thread");
    //rt = new Thread(new RepeatingThread());
    rt = new RepeatingThread();
    rt.start();
    Log.e(LOG_TAG, "Started Repeat Thread");
}

public class RepeatingThread implements Runnable {

    private final Handler mHandler = new Handler();
    private int len = 0; 


    private byte[] input = new byte[len];


    public RepeatingThread() {
        //try {
        Log.e(LOG_TAG, "Before inputJSON String");
        //inputJSON = dataInputStream.readUTF();
        //URL url = new URL("tcp://23.23.175.213:1337");
        //inputJSON = dataInputStream.readUTF();
        //inputstrrd = new InputStreamReader(socket.getInputStream());
        String hello = "hello world";
        //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));

        //Convert 
        Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
        /*}
         catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }*/

        //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);
        mHandler.postDelayed(this, 3000);       
    }
}

对于此类短期重复性任务,您可能应该使用
计时器

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TimerTask task = new RepeatingTask();
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, 0, 3000);
}

public class RepeatingTask extends TimerTask {

 private int len = 0; 

 private byte[] input = new byte[len];


 public RepeatingTask() {
     //try {
        Log.e(LOG_TAG, "Before inputJSON String");
        //inputJSON = dataInputStream.readUTF();
        //URL url = new URL("tcp://23.23.175.213:1337");
        //inputJSON = dataInputStream.readUTF();
        //inputstrrd = new InputStreamReader(socket.getInputStream());
        String hello = "hello world";
        //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));

        //Convert 
        Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 
     /*}
     catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

     //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);
 }
}


在代码中,混合了线程模式(从start()方法开始)和处理程序,这有点混乱。

您需要扩展线程,而不是实现可运行接口:

public class RepeatingThread extends Thread{
//.....
}

refreshViewModels()是否可能引发异常?我也想到了,但它甚至没有出现,因为在REPEATINGTHREAD-INPUTJSON中没有在logcat中打印出来。所以我不认为这是问题所在。但是我如何从onCreate()开始呢?我尝试了你的代码,但是出于某种原因run()方法中的代码没有执行?你知道这可能是什么原因吗?我修复了代码中的一个输入错误(
Timer Timer=new Timer();
)。我刚刚测试过它,它对我有效。。。