为什么InputStream为停止我的应用程序? [英] Why InputStream is stopping my application?

查看:136
本文介绍了为什么InputStream为停止我的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序与socket连接之间的Java服务器会谈。当服务器在运行,没有任何问题。我送从Android和服务器回答的东西。我得到响应,并把它放在烤面包。看起来很棒!但是,如果我停止正在运行的服务器,Android的住宿等待着答案,所有的应用程序停止工作。奇怪的是,应用程序在一个线程中运行,所以我认为它不应该发生的。我想知道,如果是有一些方法来避免它崩溃的应用程序和一些方法来为应用程序的用户说,服务器没有响应。对不起,如果有一些愚蠢的错误,我在Java的只是新的。在code:

 公共无效的onClick(DialogInterface对话,诠释whichButton){            InetAddress类IA;
            尝试{                IA = InetAddress.getByName(192.168.3.101);
                S =新的Socket(IA,12346);
                线程t =新主题(新doComms(S));
                t.start();
            }赶上(UnknownHostException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
        }});

和类doComms:

 类doComms实现Runnable {        私人插座S;
        串线,磷化铟;        doComms(套接字服务器){
          this.s =服务器;
        }        公共无效的run(){          INP =;          尝试{
            //从客户端获取输入
            DataInputStream以在=新DataInputStream所(s.getInputStream());
            PrintStream的出=新的PrintStream(s.getOutputStream());
            通过out.println(input.getText()的toString()+\\ n);
            而((线= in.readLine())=空&放大器;!&放大器;!。line.equals()){
              INP = INP +线;
            }            runOnUiThread(新的Runnable(){
                公共无效的run(){
                    如果(inp.equals(OK)){
                        Toast.makeText(Inicio.thisMensagem enviada玉米Sucesso的,Toast.LENGTH_LONG).show();
                    }其他{
                        Toast.makeText(Inicio.this,Falha AO投递mensagem,Toast.LENGTH_LONG).show();
                    }
               }
           });            Log.v(INP,INP);
            Log.v(类型,inp.getClass()的toString());            S.CLOSE();
          }赶上(IOException异常IOE){
            的System.out.println(IOException异常套接字听:+ IOE);
            ioe.printStackTrace();
          }
        }    }


解决方案

您正在使用的println()发送已包含一个换行符的行。这将导致发送两个新行,这可能会混淆同行,并可能导致它返回意想不到的事情,你的code可能无法正确​​地应对。

I have an Android App that talks with a Java Server between socket connection. When server is running, there's no problem. I send something from Android and server answer. I get response and put it in Toast. Looks great! But if i stop running server, Android stay waiting for answer and all application stop working. Strange is the fact that application in running inside a thread, so i think its not supposed to happen. I'd like to know if is there some way to avoid it to crash application and some way to say to app's user that server doesn't respond. I'm sorry if is there some silly mistake, i'm just new in Java. The code:

        public void onClick(DialogInterface dialog, int whichButton) {

            InetAddress ia;
            try {

                ia = InetAddress.getByName("192.168.3.101");
                s = new Socket(ia,12346);
                Thread t = new Thread(new doComms(s));
                t.start();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }});

And the class doComms:

class doComms implements Runnable {

        private Socket s;
        String line,inp;

        doComms(Socket server) {
          this.s=server;
        }

        public void run () {

          inp="";

          try {
            // Get input from the client
            DataInputStream in = new DataInputStream (s.getInputStream());
            PrintStream out = new PrintStream(s.getOutputStream());
            out.println(input.getText().toString()+"\n.");


            while((line = in.readLine()) != null && !line.equals(".")) {
              inp=inp + line;
            }

            runOnUiThread(new Runnable() {
                public void run() {
                    if(inp.equals("ok")){
                        Toast.makeText(Inicio.this,"Mensagem enviada com sucesso",Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(Inicio.this,"Falha ao enviar mensagem",Toast.LENGTH_LONG).show();
                    }
               }
           });

            Log.v("inp", inp);
            Log.v("type",inp.getClass().toString()); 

            s.close();


          } catch (IOException ioe) {
            System.out.println("IOException on socket listen: " + ioe);
            ioe.printStackTrace();
          }
        }

    }

解决方案

You're using println() to send a line that already contains a newline. That will result in two newlines being sent, which will probably confuse the peer and possibly cause it to return something unexpected, which your code may not be coping with correctly.

这篇关于为什么InputStream为停止我的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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