Android Network HTML viewer and androidhtml Viewer
This document provides an Android-based network HTML viewer.
Create a project. The project layout file is as follows:
<LinearLayout xmlns: 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 "> <EditText android: id =" @ + id/et_path "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: hint = "Enter the html path"/> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: onClick = "click" android: text = "View"/> <ScrollView android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android: id = "@ + id/TV _content" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </TextView> </ScrollView> </LinearLayout>
Create a tool class StreamTools. java:
Package com.wuyudong.html viewer. utils; import java. io. byteArrayOutputStream; import java. io. inputStream; public class StreamTools {/*** converts the content of the input stream to a String ** @ param is * @ return */public static String readInputStream (InputStream is) {try {ByteArrayOutputStream baos = new ByteArrayOutputStream (); int len = 0; byte [] buffer = new byte [1024]; while (len = is. read (buffer ))! =-1) {baos. write (buffer, 0, len);} is. close (); baos. close (); byte [] result = baos. toByteArray (); return new String (result);} catch (Exception e) {e. printStackTrace (); return null ;}}}
The complete code is as follows:
Package com.wuyudong.html viewer; import java. io. inputStream; import java. io. streamcomputedexception; import java.net. httpURLConnection; import java.net. URL; import com.wuyudong.html viewer. utils. streamTools; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. text. textUtils; import android. view. menu; import android. view. view; import android. widget. editText; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {protected static final int ERROR = 1; protected static final int SHOW_TEXT = 2; private TextView TV _content; private EditText et_path; // define a message processor private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case ERROR: Toast. makeText (MainActivity. this, "failed to get data", 0 ). show (); break; case SHOW_TEXT: String text = (String) msg. obj; TV _content.setText (text); break ;};};@ Override protected void onCreate (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 void click (View view) {final String path = et_path.getText (). toString (). trim (); if (TextUtils. isEmpty (path) {Toast. makeText (this, "path cannot be blank", 0 ). show () ;}else {new Thread () {public void run () {try {URL url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setRequestMethod ("GET"); conn. setConnectTimeout (5000); conn. setreadtimeouts (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 "); int code = 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 = new Message (); msg. what = ERROR; handler. sendMessage (msg) ;}} catch (Exception e) {e. printStackTrace (); Message msg = new Message (); msg. what = ERROR; handler. sendMessage (msg );}};}. start ();}}}