提要:我有一个AsyncTask
和doInBackground
方法,我从中调用sendHttpBuffer
将一些字符串发送到http接收器。
示例:
String[] tmp = {"TEST TO SEND","<|test123|>"};
sendHttpBuffer(connection,tmp);
... do something in between
String[] tmp2 = {"TEST TO SEND 2","<|test456|>"};
sendHttpBuffer(connection,tmp2);
问题是:多次调用sendHttpBuffer
时,只有第一个发送成功。
我想念什么吗?
private void sendHttpBuffer(HttpURLConnection connection, String[] toSend) {
if (toSend.length > 0) {
OutputStreamWriter out= null;
try {
out = new OutputStreamWriter(connection.getOutputStream());
for (String cmd : toSend ) {
if (!cmd.isEmpty()) {
out.write(cmd);
}
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
谢谢!
不要为每次调用函数创建新的OutputStreamWriter。
创建一个并在任何地方使用。