Android Web HTML Viewer

Source: Internet
Author: User

This article implements an Android-based Web HTML Viewer

New project, project layout file is as follows:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <EditTextAndroid:id= "@+id/et_path"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter HTML path" />    <ButtonAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "View" />    <ScrollViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" >        <TextViewAndroid:id= "@+id/tv_content"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" >        </TextView>    </ScrollView></LinearLayout>

The complete code is as follows:

 PackageCom.wuyudong.htmlviewer;ImportJava.io.InputStream;Importjava.io.StreamCorruptedException;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportCom.wuyudong.htmlviewer.utils.StreamTools;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.app.Activity;Importandroid.text.TextUtils;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {protected Static Final intERROR = 1; protected Static Final intShow_text = 2; PrivateTextView tv_content; PrivateEditText Et_path; //define a message handler    PrivateHandler Handler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Switch(msg.what) { CaseERROR:Toast.makeText (mainactivity. This, "Failed to get data", 0). Show ();  Break;  Caseshow_text:string TEXT=(String) msg.obj;                Tv_content.settext (text);  Break;    }        };    }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Tv_content=(TextView) Findviewbyid (r.id.tv_content); Et_path=(EditText) Findviewbyid (R.id.et_path); }     Public voidClick (View view) {FinalString Path =Et_path.gettext (). toString (). Trim (); if(Textutils.isempty (path)) {Toast.maketext ( This, "Path cannot be empty", 0). Show (); } Else {            NewThread () { Public voidrun () {Try{URL URL=NewURL (path); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); Conn.setreadtimeout (5000); Conn.setrequestproperty ("User-agent",                                "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 "); intCode =Conn.getresponsecode (); if(Code = = 200) {InputStream is=Conn.getinputstream (); String result=Streamtools.readinputstream (IS); //Tv_content.settext (result);                 message msg = new  message ();                            Msg.what = show_text; Msg.obj = result;                        Handler.sendmessage (msg); } Else{Message msg=NewMessage (); Msg.what=ERROR;                        Handler.sendmessage (msg); }                    } Catch(Exception e) {e.printstacktrace (); Message msg=NewMessage (); Msg.what=ERROR;                    Handler.sendmessage (msg);            }                };        }.start (); }    }}

Android Web HTML Viewer

Related Article

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.