[Android OutputStreamWriter多次调用时出现HttpURLConnection问题

问题描述 投票:0回答:1

提要:我有一个AsyncTaskdoInBackground方法,我从中调用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();
        }
    }
}

谢谢!

android http httpurlconnection
1个回答
0
投票

不要为每次调用函数创建新的OutputStreamWriter。

创建一个并在任何地方使用。

© www.soinside.com 2019 - 2024. All rights reserved.