如何在Android中使用HttpURLConnection类时,提取分块数据 [英] How to Extract Chunked data when using HttpURLConnection in android

查看:144
本文介绍了如何在Android中使用HttpURLConnection类时,提取分块数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用HttpURLConnection类获取数据时,我想提出一个REST API调用。

I am using HttpURLConnection to get data when i am making a REST API call.

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

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

The response that i am getting is chunked data that i found using WireShark. I want that data to be displayed in my application. How to get this chunked data using HttpURLConnection. ?? Below is response in wireshark

推荐答案

喜下面code工作在这种情况下。

Hi the below code works in this situation.

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

这篇关于如何在Android中使用HttpURLConnection类时,提取分块数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆