I have a small code to connect by Bluetooth to a device and send a small string, but apart from this, I have a JSONTask in asynchronous, and I call it onPostExecute, my problem is that, I need to call it every certain interval (waiting the refreshment of the api)
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
if(bluetoothSocket.isConnected()){
if(!outputStream.equals(null)){
while (cont<=10){
try{
new JSONTask().execute("Aqui va el link de la api");
}catch (Exception e){
e.printStackTrace();
}
new ConnectedThread().write();
txtEnviar.setText(String.valueOf(cont));
cont++;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}).start();
if(Thread.interrupted()){
try {
throw new InterruptedException();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Unfortunately, once the loop is finished, the whole program falls down, although it manages to send the result for some reason, I understand that it is not possible to call an asynctask within a thread, but how can I do this section of the code? / p>
UPDATE: apparently increasing the number of seconds solved part of the problem, I managed to send the string every so many intervals, my problem is now another, note that the api json is not being updated, it is but, apparently the application is very heavy when performing the action, so there are times when the string accumulates
UPDATE 2: Sorry for the delay with the answer, but I have not been able to solve it on my own, so I return here again, this is the error log
W/System.err: java.io.FileNotFoundException: https://api.scltrans.it/v1/stops/PF22/next_arrivals/
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:256)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
W/System.err: at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:305)
at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:291)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
I/zygote64: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
W/System.err: java.io.FileNotFoundException: https://api.scltrans.it/v1/stops/PF22/next_arrivals/
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:256)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
W/System.err: at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:305)
W/System.err: at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:291)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
I/zygote64: Do full code cache collection, code=125KB, data=104KB
I/zygote64: After code cache collection, code=115KB, data=74KB
W/System.err: java.io.FileNotFoundException: https://api.scltrans.it/v1/stops/PF22/next_arrivals/
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:256)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
W/System.err: at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:305)
at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:291)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err: at java.lang.Thread.run(Thread.java:764)
W/OkHttpClient: A connection to https://api.scltrans.it/ was leaked. Did you forget to close a response body?
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@e5b7808, channel: 1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@9b4bba1, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@2a521c6mSocket: android.net.LocalSocket@5bde587 impl:android.net.LocalSocketImpl@b340cb4 fd:java.io.FileDescriptor@84070dd, mSocketState: CONNECTED
D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@e5b7808, channel: 1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@9b4bba1, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@2a521c6mSocket: null, mSocketState: CLOSED
D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@e5b7808, channel: 1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@9b4bba1, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@2a521c6mSocket: null, mSocketState: CLOSED
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
I/Choreographer: Skipped 150 frames! The application may be doing too much work on its main thread.
I/zygote64: Do partial code cache collection, code=124KB, data=100KB
I/zygote64: After code cache collection, code=124KB, data=100KB
Increasing code cache capacity to 512KB
W/System.err: java.io.FileNotFoundException: https://api.scltrans.it/v1/stops/PF22/next_arrivals/
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:256)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
W/System.err: at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:305)
W/System.err: at com.example.nico.transapi.MainActivity$JSONTask.doInBackground(MainActivity.java:291)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
An example of the string you should send is like this
#B4P1;#B4P2;#B1P1;#B1P4;#B3P1;#B3P3;#B5P2;#B5P4;#B2P0;
but if I send the string again (assuming the JSON TASK updated the data from the api), something like this comes out
#B4P1;#B4P2;#B1P1;#B1P4;#B3P1;#B3P3;#B5P2;#B5P4;#B2P0;#B4P1;#B4
note that no number changes, and also, more characters were added at the end of the string