的Andr​​oid 2.2的readUTF()插座问题 [英] Android 2.2 readUTF() Socket Problem

查看:100
本文介绍了的Andr​​oid 2.2的readUTF()插座问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Java客户端 - 服务器程序,其中,服务器是在Windows PC上运行,并且在客户端上的Andr​​oid 2.2手机上运行。

I am trying to create a Java Client-Server Program, where the Server is running on a Windows PC, and the Client is running on an Android 2.2 Phone.

该连接是好的。从手机到PC机发送数据作品也很好。
刚接到电话的数据导致程序崩溃。

The Connection is okay. Sending Data from the Phone to the PC works also fine. Just receiving Data on the Phone crashes the program.

我使用DataInputStream和DataOutputStream类读通过Socket /写。

I am using DataInputStream and DataOutputStream to read/write through the Socket.

    //Thread on the Phone
    public void run() {
    while (RUN) {

        if (socket != null && socket.isConnected()) {
            try {
                //Crash
                String text = dis.readUTF();
                myTextView.setText(text);

            } catch (IOException ex) {
                //ErrorHandling
            }

        }

    }
}

我想从服务器接收一个​​字符串,然后显示在一个TextView中。
有任何想法吗?我已经设置此权限:

I want to receive a String from the server and then show it in a TextView. Any Ideas? I am already setting this permission:

<使用许可权的android:NAME =android.permission.INTERNET对/>

我是否需要任何其他权限?谢谢你。

do i need any other permissions? Thanks.

推荐答案

你不能,如果你在UI线程是没有设置你的用户界面的文字。

you can't set text on your UI if you're not in the UI thread.

做到这一点...

地址:

的Runnable showmessage =新的Runnable(){
        公共无效的run(){
                   myTextView.setText(membervariabletext);
            }
        };

和从你的线程,以后的readUTF(),称之为runOnUiThread(showmessage);

and from your thread, after the readUTF(), call "runOnUiThread(showmessage);"

这篇关于的Andr​​oid 2.2的readUTF()插座问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆