HTML Viewer Android

Source: Internet
Author: User

The 1.android API provides a class for accessing the network HttpURLConnection

2. Get the HTML code returned by the server by sending a GET request

3. First look at the layout file, as shown below,

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:id=" @+id/activity_main "     android:layout_width= "Match_parent"     android:layout_height= "Match_parent"      android:orientation= "Vertical" >    <EditText         android:id= "@+id/et"         android: Layout_width= "Match_parent"         android:layout_height= "wrap_content " />    <button        android:onclick= "Get"         android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"          android:text= "Get Source code"/> &nbsP;  <scrollview        android:layout_width= "Match_parent "        android:layout_height=" >    "Match_parent"      <TextView             android:id= "@+id/tv"             android: Layout_width= "Match_parent"             android: layout_height= "Match_parent"            android: Textcolor= "#00f"             android:hint= "source display"/ >    </ScrollView></LinearLayout>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

Layout looks like:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1069003/201706/ 1069003-20170623005412945-1513325335.png "style=" border:0px; "/>

4. Down is the manifest file, remember to connect the network to add permissions

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1069003/201706/ 1069003-20170623005508695-609633670.png "style=" border:0px; "/>

5.1 Look at the Java code again, mainactivity

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

package com.market.source;import android.app.activity;import android.os.bundle;import  android.util.log;import android.view.view;import android.widget.edittext;import  android.widget.textview;import java.io.ioexception;import java.io.inputstream;import  java.net.httpurlconnection;import java.net.malformedurlexception;import java.net.protocolexception; Import java.net.url;import java.net.urlconnection;import butterknife. Bindview;import butterknife. butterknife;public class mainactivity extends activity {    @ BindView (r.id.et)      EditText et;     @BindView (r.id.tv)      TextView tv;     @Override      Protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate); &nBsp;       setcontentview (R.layout.activity_main);         butterknife.bind (This);    }    public  Void get (view vew)  throws IOException {         final string str = et.gettext (). toString (). Trim ();         new thread () {            @ Override            public void run ()  {                //1. Request Address Url                url  url = null;                 try {                     url = new url (str);            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//2. Get a connection to this address                      HttpURLConnection con  =  (HttpURLConnection)  url.openconnection ();          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//3. Setting a request for this address, GET request                       Con.setrequestmethod ("GET");              &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//4. Setting Request Parameters                  &nBsp;   con.setconnecttimeout (;          ) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//5. Getting the response from the server                      int code =  Con.getresponsecode ();                     //6. According to the response, judging the success or failure of the request, 200 success                      if (code==200) {                        &NBSP;&NBSP;//7. Successful, server-class form of return data                          inputstream inputstream =  con.getinputstream ();                         //8. This flow is a byte stream that requires us to convert to a character stream before we can recognize                          final  string info = streamtool.streamtostring (InputStream);                          LOG.E ("mainactivity", info);                         runonuithread (new Runnable ()  {                               @Override                              public  Void run ()  {                                  Tv.settext (Info);                             }                          });                     }                 } catch  (malformedurlexception e)  {             &nBsp;       e.printstacktrace ();                 } catch  (protocolexception e)  {                      e.printstacktrace ();                 } catch  (ioexception e)  {                     e.printstacktrace ();                 }             }        }.start ();     }}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

5.2 Tool class for translating a stream into a string

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

public class streamtool {    public static string  Streamtostring (Inputstream inputstream)  {        int  len = -1;        byte[] buffer = new  byte[1024];        //Memory Array output stream          bytearrayoutputstream baos = new bytearrayoutputstream ();         try {             while ((Len=inputstream.read (buffer))  != -1) {                baos.write (Buffer,0,len);             }            String  Str = new string (BAOs. Tobytearray ());            return str;         } catch  (ioexception e)  {             e.printstacktrace ();         }finally {            try {                  Inputstream.close ();            } catch  (ioexception e)  {                 e.printstacktrace ();             }        }        return  null;    }}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

6. Run Effect view

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1069003/201706/ 1069003-20170623005620773-185025512.png "style=" border:0px; "/>

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/1069003/201706/ 1069003-20170623005814429-239941036.png "style=" border:0px; "/>


HTML Viewer Android

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.