Android: View webpage source code

Source: Internet
Author: User

1 Layout

<ScrollView      android:layout_width="wrap_content"      android:layout_height="wrap_content" >      <TextView          android:id="@+id/result"          android:layout_width="fill_parent"          android:layout_height="wrap_content"                     /></ScrollView>

You can embed a TextView in the ScrollView control with a scroll bar.

2. The webpage path and encoding method are used to obtain the byte array of the webpage.

public class GetPageResource {      public static String getHtml(String path,String encoding) throws Exception{      HttpURLConnection connection=(HttpURLConnection) new URL(path).openConnection();      connection.setConnectTimeout(5000);      connection.setRequestMethod("POST");     if(connection.getResponseCode()==200){     InputStream inputStream=connection.getInputStream();     byte [] imageData=GetResource.readResource(inputStream);     return new String(imageData,encoding);     }     return null;      }}

Analysis:
(1) The HttpWatch tool can be used to obtain the webpage encoding method.
(2) Use the URL to get the HttpURLConnection connection so that the resource can establish a connection and set the connection attribute value.
(3) Use HttpURLConnection connection to get the input stream. You can think like this: the webpage has been saved to the inputStream input stream.
(4) output the webpage data in the input stream to the byte array. That is, byte [] imageData = GetResource. readResource (inputStream ).
The readResource (inputStream) method is as follows:

public class GetResource {    public static byte[] readResource(InputStream inputStream) throws Exception{    ByteArrayOutputStream outputStream=new ByteArrayOutputStream();    byte [] array=new byte[1024];    int len=0;    while( (len=inputStream.read(array))!=-1){       outputStream.write(array,0,len);    }    inputStream.close();    outputStream.close();        return outputStream.toByteArray();    }

Analysis:
(1) There is no way to put the data in the input stream directly into the byte array (see the API for details), but to use ByteArrayOutputStream outputStream
Read () data into a byte array, that is, inputStream. read (buffer), and then put the data in the array
Output stream ByteArrayOutputStream in outputStream, that is, outputStream. write (buffer, 0, len );
(2) When all the data is transferred to the input stream outputStream, you can convert all the data in the output stream to a byte array, that is, outputStream. toByteArray ();
(3) In this example, the use of the input stream and the output stream is very good.

 

In the corresponding API of the input stream, the input stream is read to an array, or only one byte is read, or one row is read.
For example, in the fileinputstream class:
Public int read (byte [] B, int off, int Len) reads data of up to len bytes into one byte array from the input stream
Public int read () reads a Data byte from the input stream
For example, in the bufferedreader class:
Public String Readline () reads a text line. Return Value: string containing the content of this line

Write the byte array into the output stream in the corresponding API of the output stream, or write the data at a certain position in the array to the output stream.
For example, in the bytearrayoutputstream class method:
Public void write (byte [] B, int off, int Len) writes the len bytes starting from offset off in the specified byte array to the output stream of this byte array.
Public void write (int B) writes specified bytes to the output stream of this byte array
Then we can find that:
(1) data in the output stream can be converted into byte arrays.
For example, in the bytearrayoutputstream class method:
Public byte [] tobytearray (): Creates a new byte array. The size is the current size of the output stream, and the valid content of the buffer has been copied to the array.
(2) convert the data in the output stream to a string.
For example, in the bytearrayoutputstream class method:
Public String tostring (): converts the buffer content to a string and converts bytes to characters based on the platform's default character encoding.

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.