sách gpt4 ăn đã đi

java - android 新手 - 了解 OutputStreamWriter 在 Web 服务调用中的使用

In lại Tác giả: Walker 123 更新时间:2023-11-30 09:11:15 25 4
mua khóa gpt4 giày nike

我一直在查看 following tutorial并遇到了这段我不明白其目的的代码:

              URLConnection conn = url.openConnection(); 
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();

我不知道上面代码的目的是什么,它把这些数据写到哪里...

据我所知,文档指出它将字符转换为字节...但随后将其写入某个位置...不确定这是为什么。

câu trả lời hay nhất

它主要用于将字符流转换为字节流。

字节流和字符流对于链接不兼容,因为第一个在 8 位 ASCII 字符上运行,另一个在 16 位 Unicode 字符上运行。为了显式链接它们,java.io 包中存在两个类,InputStreamReaderOutputStreamWriter

InputStreamReader 将字节流与字符流 BufferedReader(在读取端)链接起来。

而对于 OutputStreamWriter,2 字节的字符被编码(转换)为 1 字节的字节(InputStreamReader 以另一种方式进行——字节到字符) .

对于与服务器端进程交互的 Java 程序,它必须能够写入 URL,从而向服务器提供数据。它可以按照以下步骤执行此操作:

1.Create a URL.
2.Retrieve the URLConnection object.
3.Set output capability on the URLConnection.
4.Open a connection to the resource.
5.Get an output stream from the connection.
6.Write to the output stream.
7.Close the output stream.

现在在您提供的 fragment 中,

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

creates an output stream on the connection and opens an OutputStreamWriter on it--Step 5 If the URL does support output, then this method returns an output stream that is connected to the input stream of the URL on the server side — the client's output is the server's input.If the URL does not support output, getOutputStream method throws an UnknownServiceException.

wr.write( data );
wr.close();
wr.flush();

It writes the required information to the output stream and closes the stream. The data written to the output stream on the client side is the input on the server side.

关于java - android 新手 - 了解 OutputStreamWriter 在 Web 服务调用中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22201685/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress