我是这个论坛的新手。
我目前正在编写一个与基于java的服务器通信的Android应用程序。目的是将一个片段(使用cam制作)中的照片转换为Base64-String并将它们发送到java-server。这已经全部工作,服务器和Android应用程序之间的通信工作正常。
之后服务器应该将Base64-String发送回客户端(到另一个片段)。这也很有效。
主要问题是,当客户端收到String时,我只得到一行。我想将这些行附加到一个String,但它不起作用!
主要目标是让整张照片回归。
我将不胜感激。
这是我的Class,它连接到Server并从中接收字符串。 此类运行扩展了AsyncTask。
import 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);
}
}
答案 0 :(得分:0)
你在while循环中读两行
需要更改
while ((result1 = input.readLine()) != null) {
result1 = input.readLine();
line.append(result1);
}
到
while ((result1 = input.readLine()) != null) {
line.append(result1);
}
答案 1 :(得分:0)
再次编辑我的代码。好的,你正在使用全局result1变量,它在doInBackground()中返回,并在receive()中更改,但在receive()中,它的最后一个值在从socket读取结束时将为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);
}
}