【问题标题】:Android - StringBuffer does not append incoming Strings from Server to single StringAndroid - StringBuffer 不会将来自服务器的传入字符串附加到单个字符串
【发布时间】:2013-11-24 20:13:49
【问题描述】:

我是这个论坛的新手。

我目前正在编写一个与基于 java 的服务器通信的 android 应用程序。目的是将一个片段(用 cam 制作)中的照片转换为 Base64-String 并将它们发送到 java-server。这一切正常,服务器和 android 应用程序之间的通信工作正常。

之后,服务器应该将 Base64-String 发送回客户端(发送到另一个片段)。这也很好用。

主要问题是,当客户端收到字符串时,我只得到一行。我想将这些行附加到一个字符串,但它不起作用!

主要目的是恢复整张照片。

我将不胜感激。

这是我的类,它连接到服务器并从中接收字符串。 此类运行扩展了 AsyncTask。

导入android.os.AsyncTask;

公共类 ConnectToServer 扩展 AsyncTask {

static Socket socket;
String encodedBase64;
int protocolId;
private static DataOutputStream DOS;
String result1;
String value;

public ConnectToServer(String encoded) {
    this.encodedBase64 = encoded;

}

public ConnectToServer(int i) {
    this.protocolId = i;
}

public ConnectToServer() {

}

protected String doInBackground(Void... arg0) {

    try {
        socket = new Socket("192.168.1.104", 17110);
        DOS = new DataOutputStream(socket.getOutputStream());

        if (protocolId == 1) {
            DOS.writeUTF("pictureload");
            protocolId = 0;

        } else {
            DOS.writeBytes(encodedBase64);
            receive();

        }

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result1;
}

public String receive() {

    if (socket.isConnected()) {

        try {
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

            StringBuffer line = new StringBuffer();
            while ((result1 = input.readLine()) != null) {


                    result1 = input.readLine();
                    line.append(result1);

                }

            input.close();

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

    return result1;
}

protected void onPostExecute(String result1) {
    TimelineActivity tA = new TimelineActivity();
    tA.encodeBase64(result1);
}

}

【问题讨论】:

    标签: java android stringbuffer


    【解决方案1】:

    再次编辑我的代码。好的,您正在使用全局 result1 变量,它在 doInBackground() 中返回,并在 receive() 中更改,但在 receive() 中,它的最后一个值在从套接字读取结束时将为 null。此外,在您的 receive() 函数中,您正在构建 StringBuffer 行,但最后,您将返回 String result1。下面完整代码中的所有修改和cmets...

    import android.os.AsyncTask;
    
    public class ConnectToServer extends AsyncTask {
        static Socket socket;
        String encodedBase64;
        int protocolId;
        private static DataOutputStream DOS;
    
        String value;
    
        public ConnectToServer(String encoded) {
            this.encodedBase64 = encoded;
        }
    
        public ConnectToServer(int i) {
            this.protocolId = i;
        }
    
        public ConnectToServer() {
    
        }
    
        protected String doInBackground(Void... arg0) {
            //*****local variable
            String res = null;
    
            try {
                socket = new Socket("192.168.1.104", 17110);
                DOS = new DataOutputStream(socket.getOutputStream());
    
                if (protocolId == 1) {
                    DOS.writeUTF("pictureload");
                    protocolId = 0;
                } else {
                    DOS.writeBytes(encodedBase64);
    
                    //*****lets get the string returned by receive() method
                    res = receive();
                }
    
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //*****and return it
            return res;
        }
    
        public String receive() {
            String receiveResult = null;
    
            if (socket.isConnected()) {
    
                try {
                    BufferedReader input = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));
    
                    StringBuffer line = new StringBuffer();
    
                    while ((receiveResult = input.readLine()) != null) {
                        line.append(receiveResult);
                    }
    
                    input.close();
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            //***** return your accumulated StringBuffer as string, not current line
            return line.toString();
        }
    
        protected void onPostExecute(String result1) {
            TimelineActivity tA = new TimelineActivity();
            tA.encodeBase64(result1);
        }
    }
    

    【讨论】:

    • 谢谢!我试过了,但没有用。它甚至没有给出一行。
    • 好的,它甚至进入了 try 块吗?您是否尝试过逐步调试while循环?您确定服务器正在发送数据吗?你怎么知道的?
    • 是的,他进去尝试阻止!服务器正在发送数据。我可以通过 while 循环中的这个命令在 Logcat 中读取它: System.out.println(input.readLine()) 我可以逐行读取所有接收到的数据。但我无法将它附加到一个字符串。如果我在 while 块之后尝试读取它,它不会做任何事情。 while循环是无限循环吗?谢谢
    • 非常感谢!它有效,但仅适用于 for 循环。 while 循环在我看来是一个无限循环。 :-)
    • 太棒了。你能展示你的“for”循环吗?这个 while 循环应该在流的末尾终止,并且不会无休止地运行......
    【解决方案2】:

    您在 while 循环中两次读取该行

    需要改变

      while ((result1 = input.readLine()) != null) {
          result1 = input.readLine();
          line.append(result1);
      }
    

      while ((result1 = input.readLine()) != null) {         
          line.append(result1);
      }
    

    【讨论】:

    • 我刚试了一下,但它甚至没有给出一行。还有其他建议吗?
    猜你喜欢
    • 2011-02-22
    • 2013-02-08
    • 1970-01-01
    • 2015-08-05
    • 2015-04-05
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多