android java 函数返回字符串问题
DataTranstrans=newDataTrans(Data.ip,Data.port);intret=trans.connect();if(ret!=Error.E...
DataTrans trans = new DataTrans(Data.ip, Data.port);
int ret = trans.connect();
if(ret != Error.ERROR_SUCCESS) return ret;
StringBuffer send = new StringBuffer();
send.append("orderlist");
send.append("#");
send.append(String.valueOf(count));
for(int i=0;i<count;i++){
send.append("#");
send.append(data.getSelName(i));
send.append("%");
send.append(data.getSelItemNum(i));
}
ret = trans.send(send.toString());
send = null;
if(ret != Error.ERROR_SUCCESS) return ret;
String recv = trans.recv();
trans.close();
当count较大时,即发送的字符串较长时,recv就是null
DataTrans类申明如下:
public class DataTrans {
private String ip;
private int port;
Socket socket = null;
InputStream in = null;
OutputStream out = null;
BufferedWriter bw = null;
byte[] buff = new byte[1024];
public DataTrans(String ip, int port){
this.ip = ip;
this.port = port;
}
public int connect() {
try {
socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(ip, port);
socket.connect(socketAddress,5000);
} catch (UnknownHostException e) {
e.printStackTrace();
return Error.ERROR_OPEN_SOCK;
} catch (IOException e) {
e.printStackTrace();
return Error.ERROR_OPEN_SOCK;
}
try {
out = socket.getOutputStream();
in = socket.getInputStream();
bw = new BufferedWriter(new OutputStreamWriter(out));
} catch (IOException e) {
e.printStackTrace();
return Error.ERROR_OPEN_SOCK;
}
return Error.ERROR_SUCCESS;
}
public int send(String msg){
try {
bw.write(msg);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return Error.ERROR_WRITE_SOCK;
}
return Error.ERROR_SUCCESS;
}
public String recv(){
int read = 0;
StringBuffer strBuff = new StringBuffer();
while (true) {
if (in != null) {
try {
read = in.read(buff, 0, 1024);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
if (read == -1) {
break;
}
String temp = null;
try {
temp = new String(buff, 0, read, "GBK");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
strBuff.append(temp);
temp = null;
}
return strBuff.toString();
}
public void close(){.....}
}
不知道为什么,就是count 较大时, 调试时DataTrans的recv()在返回前 strBuff是有数据的,但recv()返回给String recv为null 展开
int ret = trans.connect();
if(ret != Error.ERROR_SUCCESS) return ret;
StringBuffer send = new StringBuffer();
send.append("orderlist");
send.append("#");
send.append(String.valueOf(count));
for(int i=0;i<count;i++){
send.append("#");
send.append(data.getSelName(i));
send.append("%");
send.append(data.getSelItemNum(i));
}
ret = trans.send(send.toString());
send = null;
if(ret != Error.ERROR_SUCCESS) return ret;
String recv = trans.recv();
trans.close();
当count较大时,即发送的字符串较长时,recv就是null
DataTrans类申明如下:
public class DataTrans {
private String ip;
private int port;
Socket socket = null;
InputStream in = null;
OutputStream out = null;
BufferedWriter bw = null;
byte[] buff = new byte[1024];
public DataTrans(String ip, int port){
this.ip = ip;
this.port = port;
}
public int connect() {
try {
socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(ip, port);
socket.connect(socketAddress,5000);
} catch (UnknownHostException e) {
e.printStackTrace();
return Error.ERROR_OPEN_SOCK;
} catch (IOException e) {
e.printStackTrace();
return Error.ERROR_OPEN_SOCK;
}
try {
out = socket.getOutputStream();
in = socket.getInputStream();
bw = new BufferedWriter(new OutputStreamWriter(out));
} catch (IOException e) {
e.printStackTrace();
return Error.ERROR_OPEN_SOCK;
}
return Error.ERROR_SUCCESS;
}
public int send(String msg){
try {
bw.write(msg);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return Error.ERROR_WRITE_SOCK;
}
return Error.ERROR_SUCCESS;
}
public String recv(){
int read = 0;
StringBuffer strBuff = new StringBuffer();
while (true) {
if (in != null) {
try {
read = in.read(buff, 0, 1024);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
if (read == -1) {
break;
}
String temp = null;
try {
temp = new String(buff, 0, read, "GBK");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
strBuff.append(temp);
temp = null;
}
return strBuff.toString();
}
public void close(){.....}
}
不知道为什么,就是count 较大时, 调试时DataTrans的recv()在返回前 strBuff是有数据的,但recv()返回给String recv为null 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询