Don't mix networking and UI calls within the same thread.

The networking code should be run on its own thread, while the UI should
only be touched (calling setText, etc.) on the main thread.

These two threads can communicate using one of several available mechanisms,
e.g. by using a Handler...
 02.05.2011 14:00 пользователь "drg_c" <[email protected]> написал:
> Hi all devs,
>
> I'm currently working on a tcp client in Android.
> I want to connect my android device to a tcp server on my computer and
> receive the data once every 2 seconds. The problem is that I'm
> getting force close on my application because of the while loop that
> I've implemented in the tcp client.
> I've tried writing in different ways the loop that will make the tcp
> client checking the server socket, but with no success.
> How can I make a loop that will check the server socket without
> getting the force close?
>
> Here's my code that I'm currently using:
>
>
> public class Connection implements Runnable {
> @Override
> public void run() {
> try {
> sk=new Socket(server,port);
> viewsurface.setText("connected");
> flag = true;
> } catch (UnknownHostException e) {
> viewsurface.setText("failed 1 socket");
> flag = false;
> } catch (IOException e) {
> viewsurface.setText("failed 2 socket");
> flag = false;
> }
>
> while (flag == true){
> try {
> checkin = sk.getInputStream();
> checkint = checkin.available();
>
> if (checkint > 0){
> try {
> BufferedReader in = new BufferedReader(new
> InputStreamReader(sk.getInputStream()));
> received = in.readLine();
> viewsurface.setText(received);
>
> } catch (IOException e) {
> viewsurface.setText("failed to receive");
> }
> }
>
> Thread.sleep(2000);
> } catch (IOException e) {
> viewsurface.setText("checkin failed");
>
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
>
> }
> }
> }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to