ive been thinking about this for hours and im not closer to an solution!
My thread just stops looping when im fetching a message from an server for some reason, and works perfectly when im not doing it.
This works and prints refreshing every second:
public class ChatRoom extends Activity implements OnClickListener, Runnable {
private Thread t = new Thread(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chatroom);
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(this);
Intent receiver = getIntent();
String host = receiver.getStringExtra("Host");
int port = receiver.getIntExtra("Port", 4456);
try
{
socket = new Socket(host, port);
this.receive = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
this.send = new PrintWriter(this.socket.getOutputStream(), true);
}
catch(IOException ioe) { System.out.println(ioe); }
t.start();
}
public void run()
{
String message = "";
while(true)
{
try
{
// message = receive.readLine(); BufferedReader
t.sleep(1000);
}
//catch(IOException ioe) { System.out.println(ioe); }
catch (NullPointerException npe) { System.out.println(npe); }
catch (InterruptedException e) { System.out.println(e); }
System.out.println("Refreshing...");
}
}
And when i use my commented code, it actually works and i get a message from the server but it loops just once! Why is that?
Output:
Server Message
Refreshing...
I get no Exception or errors, but i had an error before with some similar code that said that i cant change UI on other threads. So ive been looking at some runOnUiThread but it didnt make it better, and i dont know why it should :(
The method BufferedReader.readLine() blocks until a newline character is received. If there is no newline in your receiver stream it will block forever.
A few things here:
Swap from System.out.println("string"); to Log.d("tagname","string"); then look on DDMS for output lines.
I don't think you're creating a thread properly, and you certainly aren't providing any interface to kill it, which may cause issues when you test it. I would separate the thread into a new file, say NameOfThread:
//File "NameOfThread"
public class NameOfThread extends Thread{
//any fields you want here to mess with e.g.
private String message;
private boolean running;
public NameOfThread(){
message = "";
running = true;
}
#Override
public void run(){
while(running){
//do stuff
}
}
public void setRunning(boolean run){
running = run;
}
}
//When you want to call it
NameOfThread varThread = new NameOfThread();
varThread.start();
//when you want to kill the thread
varThread.setRunning(false);
You may think 'why bother with this whole running variable junk, I don't need it.' but how else will this thread end gracefully? There is another method of killing the thread properly, which is using InterruptedException and your cleanup code goes there, but that's just an alternative.
Try doing this first, then you'll need to sort out the message itself (the method you're using currently isn't great since readLine() will block until a line is received (meaning you'll get "Refreshing..." when you get a new line rather than once per second.
You're surely getting some exceptions thrown, you just can't see them cause you're trying to print them on the standard output, which is missing on Android. Your exception is handled correctly and the code finishes. To properly get the exception information use Logs, or just throw a RuntimeException. Hope this helps.
Related
My USB Host is receiving sensor data and it is getting updated every 200ms. I would like to read this data in my android app every 200ms. I am able to read it using bufferreader, It reads the data for sometime and then hangs. It is not consistent. I am new to this and may be I am not doing it the correct way. Below please find my code and let me know your suggestions. Thanks in advance.
public void startProcessOne()
{
new CountDownTimer(110,100)
{
#Override
public void onTick(long millisUntilFinished)
{
StringBuilder text = new StringBuilder();
line = "";
try {
FileReader in = new FileReader("/mnt/udisk/TEST.TXT");
BufferedReader br = new BufferedReader(in);
int i=0;
char[] buf = new char[10000];
while((i = br.read(buf,i,100))!= -1)
{
String h = new String(buf);
text.append(h);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
TxtRead.setText(text.toString());
}
#Override
public void onFinish()
{
startProcessOne();
}
}.start();
}
TxtRead.setText(text.toString());
This line is causing the problem. You can't touch UI elements from a background thread. You should instead run those codes in the UI/Main thread.
In your case, I'd personally prefer using Java threads. So, create a background thread to keep running periodically. If you would need to run UI methods from that background thread. You probably need a handler attached to the main thread.
// Instantiate a handler in UI thread
final Handler handler = new Handler();
new Thread(new Runnable(){
// Once you're done and want to break the loop, just set this boolean
private boolean stopped = false;
#Override
public void run(){
while(!stopped) {
// Read from the file
// Whenever you need to update an UI element,
// you should wrap it inside this runnable object
handler.post(new Runnable(){
#Override
public void run(){
// Update UI
TxtRead.setText("new_text");
}
})
try {
// This thread will sleep for 9 seconds
Thread.Sleep(9000);
} catch(Exception e){}
}
}
}).start();
I keep getting an illegal argument exception when running my app. However, this happens prior to the Toast messages coming up and that's why I think I need a delay.
Prior to adding on the DatabaseHelper class, my app was running and the proper value was coming up on both Toast messages, the one in the MainActivity and the one showing the intent value passed in the DisplayResult activity.
I'm not sure what to do at this point.
Just do a thread sleep in a runnable.
int timeYouWantToSleep = 60000;
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(timeYouWantToSleep);
//do your work here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
If this doesn't work, you know it's not a delay that you need.
im making an app that should send a message after certain amount of time via bluetooth.
The thread works fine when i use "Log" but when i use the write function of bluetooth, it seems to executes the loop when the app wants.
Hope somebody can help me:
public class AsyncWrite extends Thread {
#Override
public void run() {
while(true) {
try {this.sleep(250);} catch (Exception E) {}
//Log.e(TAG,"Test run");
mConnectedThread.write(A);
}
}
}
I've already try using asynctask executors and handlers, the result it's the same.
I created a new thread to handle my TCP management needs. I did this in order to keep the UI thread active while it's doing all sorts of socket magic. The problem I'm having is that the variable num used in ServerThread doesn't seem to updating within the handler.post()'s run method. It changes once but subsequent iterations within my while(true) loop no longer changes its value. However, outside handler.post() I noticed that it is changing properly. I have included a log command for logcat in order to see the values of num so that's why I know this is what is happening. The thing is, I need the variable num inside the run() method to update a few things in the UI thread.
May be there's something wrong with the way I declared num. I did not include in the code most of the things I think is irrelevant to my question, so if I missed something please let me now. This is my first adventure into android java threading so some help would be really appreciated. Just run out of mental energy tonight.
public class MainActivity extends Activity {
private int num = 0;
private Handler handler = new Handler();
//Other declarations
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Do bunch of things including starting ServerThread
//and creating a TCP server socket
}
public class ServerThread implements Runnable{
public void run(){
// ...
// Do bunch of stuff including waiting for a client to connect
try{
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while(true){
num = in.read();
Log.e("MainActivity", "A:" +num); //<-- WORKS FINE
if(num == -1) //client socket closed
break;
handler.post(new Runnable() {
#Override
public void run() {
Log.e("MainActivity", "B:" +num); //<-- DOES NOT WORK FINE
//Do bunch of stuff here with the variable num
//including updating the UI thread
}
}
}catch(Exception e){
// ...
}
}
}
I am trying to make a simple app. Where user sees one edittext .. enters some text in it.. and then press send... then the server on laptop receives that message.
Now the NetworkOnMainThread exception is giving me Headaches......the app works perfectly for 2.3.3 because there was no such thing as NetworkOnMainThread Exception that time.
Having searched a lot .. Two solutions are
Making new thread for networking OR
AsyncTask.
I tried both without any results.
Try 1: With Separate Thread:
Now what I could understand is that I had to start a separate thread. Ok. I did.
Following is my client side code.
EditText e ;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e= (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
Thread startNetworking = new Thread(new NetworkThread());
startNetworking.start();
}
public void sendMessage(View v){
if(NetworkThread.sendToClient(e.getText().toString()))
tv.setText("Status : Successful");
else
tv.setText("Status : Unsuccessful");
}
sendMessage is onClick function for my send button. I have another JAVA file NetworkThread.java....
Here is a code for that :
public class NetworkThread implements Runnable{
static DatagramSocket socket;
static InetAddress add;
public void run() {
try {
socket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
add = InetAddress.getByName("192.168.1.12");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean sendToClient(String message){
DatagramPacket p = new DatagramPacket(message.getBytes(),message.getBytes().length,add,4444);
try {
socket.send(p);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
This is still doesn't work. I first want to exhaust this first try then I will move onto asking here about AsyncTask and what I have tried. So for time being please help me achieve this simple task of sending and receiving a string.
Unfortunately, having sendToClient() defined in the same class as NetworkThread doesn't mean that it will run on your network-specific thread. The sendToClient() method will still run on your main (UI) thread because it's being called from your onClick() method. UI callbacks, such as onClick(), are always processed on the UI thread.
I would recommend using an AsyncTask as it enables you to send arbitrary data (such as your message parameter) to the background/network thread before it executes. Continuing to use Runnable and Thread will require extra machinery to synchronize the execution of your UI and network threads, and these challenges are handled behind the scenes by AsyncTask.