我是这个论坛的新手。
我目前正在编写一个与基于 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/