1

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

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

enter image description here

laalto
  • 150,114
  • 66
  • 286
  • 303
tech spartans
  • 370
  • 6
  • 22
  • possible duplicate of [Java: How to download chunked content correctly?](http://stackoverflow.com/questions/5737945/java-how-to-download-chunked-content-correctly) – Brian Roach May 11 '13 at 06:51

1 Answers1

3

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

Copy Function

      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);
           }
        }       
tech spartans
  • 370
  • 6
  • 22