我在进行REST API调用时使用HttpURLConnection来获取数据。
HttpURLConnection connection = (HttpURLConnection) (new URL("http://" + account + ".table.core.windows.net/"+urlPath)).openConnection();
connection..connect();
我得到的响应是使用WireShark找到的分块数据。 我希望数据显示在我的应用程序中。如何使用HttpURLConnection获取这个分块数据。 ?? 以下是wireshark的回复
答案 0 :(得分:3)
以下代码适用于这种情况。
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);
}
}