Android Download Web text

Source: Internet
Author: User

The specific interface is as shown

To download this file from your browser to your phone, follow these steps:

1, a new project named Gettxtdata;

2, modify the Res/layout/fragment_main.xml file, the specific code is as follows:

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.example.gettxtdata.mainactivity$placeholderfragment" >Ten  One     <LinearLayout A         Android:layout_width= "Fill_parent" - Android:layout_height= "Fill_parent" - android:orientation= "vertical" the         > -         <EditText -             Android:id= "@+id/inputtext" - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" - Android:text= "Http://www.sohu.com" +             /> A         <Button at             Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:id= "@+id/gettxtbtn" - Android:text= "Get Text" -             /> -         <ScrollView in             Android:layout_width= "Fill_parent" - Android:layout_height= "Fill_parent" to             > +             <TextViewAndroid:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" the Android:id= "@+id/sohu" *                 /> $         </ScrollView>Panax Notoginseng     </LinearLayout> -  the  + </Relativelayout>
View Code

3. Create a new class named Nettool.java, which is primarily manipulating text, and the methods added in the class are as follows:

1  Public StaticString gethtml (String path,string enconding)throwsexception{2         //Create a URL class object, throw an exception3URL url=NewURL (path);4         //Get HttpURLConnection Object5HttpURLConnection conn=(HttpURLConnection) url.openconnection ();6Conn.setrequestmethod ("GET");//Claim Request Method7Conn.setconnecttimeout (6*1000);//Set link Timeout8         9         if(Conn.getresponsecode () ==200)Ten         { OneInputStream Istream=conn.getinputstream ();//Get input stream A             byte[] Data=readinstream (IStream);//Get the data -              -             //converting bytes to Strings the             return NewString (data,enconding); -}
View Code

4, modify the Mainactivity.java code, the code is as follows:

1  Public classMainactivityextendsactionbaractivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8         if(Savedinstancestate = =NULL) {9 Getsupportfragmentmanager (). BeginTransaction ()Ten. Add (R.id.container,Newplaceholderfragment ()). commit (); One         } A     } -  - @Override the      Public BooleanOncreateoptionsmenu (Menu menu) { -  -         //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); +         return true; -     } +  A @Override at      Public Booleanonoptionsitemselected (MenuItem item) { -         //Handle Action Bar item clicks here. The Action Bar would -         //automatically handle clicks on the Home/up button, so long -         //As you specify a the parent activity in Androidmanifest.xml. -         intID =Item.getitemid (); -         if(id = =r.id.action_settings) { in             return true; -         } to         return Super. onoptionsitemselected (item); +     } -  the     /** * * A placeholder fragment containing a simple view. $      */Panax Notoginseng @TargetApi (build.version_codes. Gingerbread) -      Public classPlaceholderfragmentextendsFragment { the  +          Publicplaceholderfragment () { A         } the  + @Override -          PublicView Oncreateview (layoutinflater inflater, ViewGroup container, $ Bundle savedinstancestate) { $View Rootview =inflater.inflate (R.layout.fragment_main, container, -                     false); -Strictmode.setthreadpolicy (New  the StrictMode.ThreadPolicy.Builder (). Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). Build ()); - Strictmode.setvmpolicy (Wuyi                      NewStrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects (). Detectleakedclosableobjects (). Penaltylog (). Penaltydeath (). build ()); the  -              Wu             FinalEditText inputtext=(EditText) Rootview.findviewbyid (r.id.inputtext); -             FinalButton getbtn=(Button) Rootview.findviewbyid (R.ID.GETTXTBTN); About             FinalTextView txtview=(TextView) Rootview.findviewbyid (R.id.sohu); $              -Getbtn.setonclicklistener (NewView.onclicklistener () { -                  - @Override A                  Public voidOnClick (View v) { +                     //TODO auto-generated Method Stub theString path=Inputtext.gettext (). toString (); -                     Try $                     { theString html=nettool.gethtml (Path, "GBK"); the Txtview.settext (HTML); the                          theToast.maketext (mainactivity. This,  -"Get text Success", Toast.length_long). Show (); in}Catch(Exception e) the                     { theToast.maketext (mainactivity. This,  About"Get text Failed", Toast.length_long). Show (); the e.printstacktrace (); the                     } the                 } +             }); -             returnRootview; the         }Bayi     } the  the}
View Code

5. Modify the Androidmanifest.xml code and add the following code after the </application> tag:
<uses-permission android:name= "Android.permission.INTERNET"/>

Android Download Web text

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.