Android URL Read getHtmlByteArray(final String url)

Here you can find the source of getHtmlByteArray(final String url)

Description

get Html Byte Array

Declaration

public static byte[] getHtmlByteArray(final String url) 

Method Source Code

//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static byte[] getHtmlByteArray(final String url) {
        URL htmlUrl = null;/*  w w w  .j  a va2 s  . c om*/
        InputStream inStream = null;
        try {
            htmlUrl = new URL(url);
            URLConnection connection = htmlUrl.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.getInputStream();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] data = inputStreamToByte(inStream);

        return data;
    }

    public static byte[] inputStreamToByte(InputStream is) {
        try {
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) != -1) {
                bytestream.write(ch);
            }
            byte imgdata[] = bytestream.toByteArray();
            bytestream.close();
            return imgdata;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. getXML(String url)
  2. readXmlFromUrl(String address)
  3. getStringFromUrl(String url)
  4. getFileDateTime(URL url)
  5. readTextFromUri(Context c, Uri uri)