سؤال

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

هل كانت مفيدة؟

المحلول

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);
           }
        }       
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top