Java 在android中使用HttpURLConnection时如何提取分块数据
在进行RESTAPI调用时,我使用HttpURLConnection获取数据Java 在android中使用HttpURLConnection时如何提取分块数据,java,android,rest,chunked-encoding,Java,Android,Rest,Chunked Encoding,在进行RESTAPI调用时,我使用HttpURLConnection获取数据 HttpURLConnection connection = (HttpURLConnection) (new URL("http://" + account + ".table.core.windows.net/"+urlPath)).openConnection(); connection..connect(); 我得到的响应是我使用WireShark发现的分块数据。 我希望这些数据显示在我的应用程序中。如何使用
HttpURLConnection connection = (HttpURLConnection) (new URL("http://" + account + ".table.core.windows.net/"+urlPath)).openConnection();
connection..connect();
我得到的响应是我使用WireShark发现的分块数据。
我希望这些数据显示在我的应用程序中。如何使用HttpURLConnection获取此分块数据??
以下是wireshark的回应
嗨,下面的代码在这种情况下工作
HttpURLConnection connection = (HttpURLConnection) (new URL("http://" + account + ".table.core.windows.net/"+urlPath)).openConnection();
signRequestSK(connection, account, key);
connection.connect();
ByteArrayOutputStream sink = new ByteArrayOutputStream();
copy(connection.getInputStream(), sink, 3000);
byte[] downloadedFile = sink.toByteArray();
String str = new String(downloadedFile, "UTF8");
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage(str);
alert.show();
复制功能
public static void copy(InputStream in, OutputStream out , int bufferSize)
throws IOException
{
// Read bytes and write to destination until eof
byte[] buf = new byte[bufferSize];
int len = 0;
while ((len = in.read(buf)) >= 0)
{
out.write(buf, 0, len);
}
}
可能重复的