Note that there are some explanatory texts on larger screens.

plurals
  1. POrun() method in Timer Task is not executing - Android
    primarykey
    data
    text
    <p>InputStream and OutputStream Declarations:</p> <pre><code>public OutputStream out = null; public InputStream inputStr = null; </code></pre> <p>onCreate() code starting the TimerTask:</p> <pre><code>@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"); out = socket.getOutputStream(); inputStr = socket.getInputStream(); } </code></pre> <p>Timer Task Code: </p> <pre><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); } } </code></pre> <p>inputStr is an InputStream used to read the bytes and convert them into a string. </p> <p>ConvertByteArrayToString() just converts the Byte Array to String &amp; ConvertStringToByteArray() converts a String into a Byte[] so that the data can be sent using sendBytes(byte[], int, int). </p> <p>I'm trying to figure out why my code gets stuck in this try/catch statement without any exceptions being thrown:</p> <pre><code> try { inputJSON = ConvertByteArrayToString(readBytes(inputStr)); sendBytes(ConvertStringToByteArray(inputJSON), 0, ConvertStringToByteArray(inputJSON).length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>Here is my sendBytes() method:</p> <pre><code>public void sendBytes(byte[] myByteArray, int start, int len) throws IOException { if (len &lt; 0) throw new IllegalArgumentException("Negative length not allowed"); if (start &lt; 0 || start &gt;= 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 &gt; 0) { dos.write(myByteArray, start, len); } } </code></pre> <p>Here is my readBytes() method:</p> <pre><code>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 &gt; 0) { dis.readFully(data); } return data; } </code></pre> <p>Here is the code for the ConvertByteArrayToString() and ConvertStringToByteArray() methods:</p> <pre><code>public String ConvertByteArrayToString(byte[] b) { // byte[] to string String input = new String(b); return input; } public byte[] ConvertStringToByteArray(String str) { // string to byte[] byte[] bytes = str.getBytes(); return bytes; } </code></pre> <p>Here is the logcat output I'm seeing:</p> <pre><code> E/AndroidClient(17109): Start Repeat Timer E/AndroidClient(17109): Before inputJSON String D/WindowManager( 110): Home Key Test: false :false </code></pre> <p>Any help or pointers in the right direction would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload