简体   繁体   English

Android 应用程序未从套接字输入读取 stream

[英]Android application not reading from Socket input stream

My android application is not receiving any data from my server, for some reason.由于某种原因,我的 android 应用程序没有从我的服务器接收任何数据。 Strangely enough, a different socket client I wrote (which runs on my computer rather than an AVD) receives and prints all server-sent messages without fault.奇怪的是,我编写的一个不同的套接字客户端(它在我的计算机上运行,而不是在 AVD 上运行)接收并打印所有服务器发送的消息而没有错误。 It uses similar code to what is held within the doInBackground method.它使用与doInBackground方法中的代码类似的代码。

public class Client extends AsyncTask<Void, Void, Void>  {

    int port;
    Socket s;

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            port = 1818;
            s = new Socket("xx.xx.xx.xx", port);
            if (!s.isConnected()) {
                s.close();
            }

            BufferedReader re = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String temp = null;

            while ((temp = re.readLine()) != null)
            {
                MainActivity.changeT(temp); // This will replace the TextView's text with temp.
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

I am thinking a possible solution would be to place the while loop into a separate thread, but I am not sure.我在想一个可能的解决方案是将while循环放入一个单独的线程中,但我不确定。 Any suggestions are welcome: :-)欢迎任何建议::-)

While the application was accessing the server appropriately, MainActivity.changeT(temp);当应用程序适当地访问服务器时, MainActivity.changeT(temp); is attempting to access the UI thread from a background thread, which is not appropriate.正在尝试从后台线程访问 UI 线程,这是不合适的。

I solved this by passing an instance of the MainActivity to this Client class, and I used the runnable method runOnUiThread(...) .我通过将 MainActivity 的一个实例传递给这个客户端 class 解决了这个问题,并且我使用了runnable的方法runOnUiThread(...)

public class Client extends AsyncTask<Void, Void, Void> {

    int port;
    Socket s;
    MainActivity instance;

    Client(MainActivity instance)
    {
        this.instance = instance;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            port = 1818;
            s = new Socket("xx.xx.xx.xx", port);
            if (!s.isConnected()) {
                s.close();
                return null;
            }

            BufferedReader re = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String temp = null;
            TextView t = instance.getT(); // Accesses a getter method that returns the TextView.

            while ((temp = re.readLine()) != null)
            {
                setText(t, temp); // Accesses the UI Thread and changes the TextView.
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    private void setText(final TextView text,final String value){
        instance.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                text.setText(value); // This is equivalent to writing the code in the UI thread itself.
            }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM