java - Android - StringBuffer 不会将从服务器传入的字符串附加到单个字符串

标签 java android stringbuffer

我是这个论坛的新手。

我目前正在编写一个与基于 java 的服务器通信的 Android 应用程序。目的是将一个 fragment 中的照片(用 cam 制作)转换为 Base64 字符串并将其发送到 java 服务器。这一切都已准备就绪,服务器和 Android 应用程序之间的通信工作正常。

之后,服务器应将 Base64 字符串发送回客户端(发送到其他 fragment )。这也运作良好。

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

主要目标是取回整张照片。

我希望得到一些帮助。

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

导入 android.os.AsyncTask;

公共(public)类 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);
}

}

最佳答案

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

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);
    }
}

关于java - Android - StringBuffer 不会将从服务器传入的字符串附加到单个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19933259/

相关文章:

java - 检测 TCP 连接重置的方法

java - @JoinColumnS 涉及一对一关系中的 EAGER 获取

android - 我无法将启动画面中的默认 flutter 图标更改为我在 flutter 中选择的图像

java - Java 基准测试(比较两个类)

java - 字符串缓冲区到字符串转换异常?

java - 有什么简单的方法可以调整一段文字的字体大小以适应特定区域?

java - 在 Android 应用程序中导入 Java 项目?

android - 如何在 android 中使用 addTextChangedListener 处理多个 EditText

android - Retrofit 2 Request适用于Postman,但不适用于App

java - 什么需要将 StringBuffer 绑定(bind)到同步块(synchronized block)?