1

我是这个论坛的新手。

我目前正在编写一个与基于 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);
}

}

4

2 回答 2

0

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

需要改变

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

  while ((result1 = input.readLine()) != null) {         
      line.append(result1);
  }
于 2013-11-12T15:40:56.003 回答
0

再次编辑我的代码。好的,您正在使用全局 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);
    }
}
于 2013-11-12T16:07:35.190 回答