Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:54:28+00:00 2026-06-10T08:54:28+00:00

onCreate() code starting the TimerTask : @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

  • 0

onCreate() code starting the TimerTask:

@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");
}

Timer Task Code:

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 is an InputStream used to read the bytes and convert them into a string.

ConvertByteArrayToString() just converts the byte array to String & ConvertStringToByteArray() converts a String into a Byte[] so that the data can be sent using sendBytes(byte[], int, int).

I’m trying to figure out why my code gets stuck in this try/catch statement without any exceptions being thrown:

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

Here is my sendBytes() method:

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

Here is the getPayloadStr() method:

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

Here is my readBytes() method:

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

Any help or pointers in the right direction would be appreciated. Any help via chat would be greatly appreciated too. Whole file could be posted or viewed in chat session if needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:54:30+00:00Added an answer on June 10, 2026 at 8:54 am

    My idea it is :

    • leave it emprty the conrructor and set a breakpoint to run() -hopefully hitted, or you will see the IN REPEATINGTHREAD-INPUTJSON message in log. If not, than I don’t know where is the problem, because the code it seems to be ok.
    • if you see, than in the contructor is the problem( problem1 isolated)

    I think here it is the problem:

    int len = dis.readInt();

    reading it is a blocking method, so your code in Even dispatcher thread or Main thread or Ui thread, – forgot what name is that on that platform- is waiting probably, until he can read 4 bytes.

    Based on comment and code request here it is:

    @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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code looks as follows.. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try
My timer code is: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initializer(); fire.setOnClickListener(this); }
Here is my code: MediaPlayer a1,a2,a3...a24; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a);
My Code:: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); VideoView videoView = (VideoView) findViewById(R.id.videoView1);
My Code:- public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); // defining the WebView WebView
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Init(); backthread tr=new backthread(); tr.execute(0,0,0); } backthread
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); savedInstanceState.putString(foo, bar); } @Override public void
my code is public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { Log.d(response, starting city
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); TextView lattv=(TextView)findViewById(R.id.lat); TextView lngtv=(TextView)findViewById(R.id.lng); JSONParstring jParser =
Here is the relevent code: ... Resources res; @Override public void onCreate(Bundle savedInstanceState) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.