如何在Android中使用HttpURLConnection的时候提取分块数据(How to Ext

2019-10-18 03:32发布

我使用HttpURLConnection的时候我想提出一个REST API调用来获取数据。

HttpURLConnection connection = (HttpURLConnection) (new URL("http://" + account + ".table.core.windows.net/"+urlPath)).openConnection();
connection..connect();

那我得到的响应分块,我发现使用Wireshark的数据。 我想,要在我的应用程序中显示的数据。 如何获取使用HttpURLConnection类分块数据。 ?? 下面是Wireshark的响应

Answer 1:

嗨下面的代码工作在这种情况下。

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);
           }
        }       


文章来源: How to Extract Chunked data when using HttpURLConnection in android