java - Socket客户端android不显示收到的消息

标签 java android sockets server client

我的代码有问题 我创建了一个应用程序,通过单击

向服务器发送消息或从服务器接收消息

按钮

发送消息效果非常好 但收到时它没有向我显示任何内容 我希望应用程序在与服务器通信期间发送和接收并显示消息时向我显示消息,因为我刚刚注意到在通信过程中应用程序无法显示任何内容( toast 或更改 TextView/EditText 或任何内容的文本),期望只接收不显示变量数据 但我需要应用程序在与服务器通信期间执行很多操作(显示 toast 和打开对话框等..)

点击代码:

EditText nom ; //nom= (EditText) findViewById(R.id.user); is in Oncreate methode

String name,check;
String check;
 public void click(View view) {


    name=nom.getText().toString();



    Thread t=new Thread(new Runnable() {
        @Override
        public void run() {

            Socket s;


            try {

                s = new Socket(ipadresse,Integer.parseInt(por)); 
//those 2 (ip,por) are in methode (oncreate)

                DataOutputStream dos=new DataOutputStream(s.getOutputStream());



                //emission just du nom (un exemple)
                dos.writeUTF(String.valueOf(name));
                //sending message

                //recieving a message :



                DataInputStream dis=new DataInputStream(s.getInputStream());
                check=dis.readUTF();


                //i've tried this command but not work :/ 
                //BufferedReader read=new BufferedReader(
                //new InputStreamReader(s.getInputStream()));

                //check= read.readLine();

                Log.i("message", "message recieved : "+check);

//logcat show me that the message recieved but toast doesn't 
//( in fact i didn't want toast i want to show a dialog box 
//but as a simple example i used toast)

                Toast.makeText(login.this, check, Toast.LENGTH_SHORT).show();



                dos.flush();

                //dis.close();
                dos.close();
                s.close();



            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    });


    t.start();




}

这里是服务器代码:

public static void main(String[] args) {

    Thread t;


        t = new Thread(new Runnable() {
        @Override
        public void run() {
           try {
                    ServerSocket s = new ServerSocket(8080);
                    System.out.println("connecting  ... ");
                    System.out.println("-------------------");


                        while(true) 
                        {
                            Socket s1= s.accept(); 

                           DataInputStream dis=new DataInputStream(s1.getInputStream());

                           DataOutputStream dos=new DataOutputStream(s1.getOutputStream());


                            String message;





                              message=dis.readUTF();

                           System.out.println(message);   


                           //this message does't displayed in the app (i've edited the program just to simplify it)    
                           dos.writeUTF("hello");



                            dis.close();
                            s1.close();
                        }


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        });
    t.start();
}

最佳答案

也许来自您服务器的消息会延迟返回(不是实时)。 尝试制作循环:

boolean getmessage = false;
    while(!getmessage){
        try{
            check=dis.readUTF();
        }
        catch(Exception e){
            Log.d("dis", e.getMessage());
        }
        if(check != null){
            getmessage = true;
            makeToast("23");
        }
    }

关于java - Socket客户端android不显示收到的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42914093/

相关文章:

java - ApplicationInfo 元数据返回 null

sockets - window 电话 7 : Socket Programming

java.lang.NullPointerException 通过对象输出流发送消息时

java - JSTL fmt 库抛出 500 错误,需要启用 session ?

java - AlertDialog 错误字符串资源 ID #0x1

java - servlet 中的 httpClient post 调用失败,错误为 java.lang.NoSuchMethodError :

Android ListActivity - 在空列表和非空列表之间切换

android - 计算器在某些设备上不起作用

c - 为什么第一个客户端看到源 ip 为 0.0.0.0?

java - 我的飞利浦 Hue 灯闪烁