代号一 - 无法从真实设备上的套接字读取

Codename One - Can't read from socket on real device

我正在开发一个 iOS 应用程序,使用代号一。我扩展了 SocketConnection class,以便从服务器接收数据。

class CustomSocketConnection extends SocketConnection {

    private OutputStream os;
    private InputStream is;
    private InputStreamReader isr;
    private String rawMessage;

    public CustomSocketConnection(){
        os = null;
        is = null;
        isr = null;
        rawMessage = null;
    }

    public synchronized String getRawMessage(){
        return rawMessage;
    }

    @Override
    public void connectionError(int errorCode, String message) {
        rawMessage = null;
        ToastBar.showErrorMessage("Error Connecting. ErrorCode: " + errorCode + " Message: " + message);
        System.out.println(">>>CustomSocketConnection, connectionError. Error Connecting. ErrorCode: " + errorCode + " Message: " + message);
    }

    @Override
    public void connectionEstablished(InputStream is, OutputStream os) {
        System.out.println(">>>CustomSocketConnection, connectionEstablished");

        char termination = '\n';

        int length = 1024;
        char[] buffer = new char[length];
        byte[] bufferByte = new byte[length];
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println(">>>CustomSocketConnection, connectionEstablished, prima del while");

        try {
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            System.out.println(">>>CustomSocketConnection, connectionEstablished, prima della read");

            while(true){
                System.out.println(">>>CustomSocketConnection, loop di read, inizio");
                int read = isr.read();
                System.out.println(">>>CustomSocketConnection, loop di read, subito dopo la read");
                char c = (char) read;
                if (read == -1) rawMessage = null;

                System.out.println(">>>CustomSocketConnection, loop di read, letto: " + c);

                while(c != termination){
                    stringBuilder.append(c);
                    read = isr.read();
                    c = (char) read;
                    if (read == -1) rawMessage = null;
                }
                rawMessage = stringBuilder.toString();
                if(rawMessage != null) doActions(rawMessage);

                //System.out.println(">>>CustomSocketConnection, connectionEstablished, ho letto: " + rawMessage + "FINE");
            }


        } 
        catch (IOException ex) {
            System.out.println(">>>CustomSocketConnection, connectionEstablished, errore: " + ex.getMessage());
        }

        System.out.println(">>>CustomSocketConnection, connectionEstablished, dopo il while"); 

    }

    private void doActions(String msg){
        //do something

    }
}

connectionEstablished 方法中,我使用 InputStreamReader class.

的读取方法从服务器读取数据

如果我 运行 模拟器上的应用程序,它可以正常工作并从服务器接收数据。当我在真实设备上启动应用程序时(iPad mini,32 位设备,iOS 版本 8.1.1 12B435,more details here;iPhone 7s,64 位设备, iOS version 11.2.5 15D60), read 方法不从服务器接收数据。其实在read方法之前可以看到println打印的字符串,但是在read方法之后看不到println打印的字符串

问题不在服务器端,因为我开发了一个 Android 应用程序,它从同一台服务器接收数据。没有防火墙限制或其他网络限制:Android 应用程序和 Codename One 模拟器在连接到服务器的同一本地网络或从另一个服务器连接时都会接收数据。

怎么了?

使用 InputStream 而不是 InputStreamReader 已解决。

@Override
public void connectionEstablished(InputStream is, OutputStream os) {
    System.out.println(">>>CustomSocketConnection, connectionEstablished");

    char termination = '\n';

    StringBuilder stringBuilder = new StringBuilder();
    System.out.println(">>>CustomSocketConnection, connectionEstablished, prima del while");

    try {
        System.out.println(">>>CustomSocketConnection, connectionEstablished, prima della read");

        while(true){
            System.out.println(">>>CustomSocketConnection, loop di read, inizio");
            int read = is.read();
            System.out.println(">>>CustomSocketConnection, loop di read, subito dopo la read");
            char c = (char) read;
            if (read == -1) rawMessage = null;

            System.out.println(">>>CustomSocketConnection, loop di read, letto: " + c);

            while(c != termination){
                stringBuilder.append(c);
                read = is.read();
                c = (char) read;
                if (read == -1) rawMessage = null;
            }
            rawMessage = stringBuilder.toString();
            if(rawMessage != null) doActions(rawMessage);

            //System.out.println(">>>CustomSocketConnection, connectionEstablished, ho letto: " + rawMessage + "FINE");
        }


    } 
    catch (IOException ex) {
        System.out.println(">>>CustomSocketConnection, connectionEstablished, errore: " + ex.getMessage());
    }

    System.out.println(">>>CustomSocketConnection, connectionEstablished, dopo il while"); 

}

任何人都可以向我解释为什么它与 InputStream 一起工作吗?