Android downloads network text and android Network text

Source: Internet
Author: User

Android downloads network text and android Network text

Shows the specific interface.

To download a text file from your browser to your mobile phone, follow these steps:

1. Create a project named GetTxtData;

2. Modify the res/layout/fragment_main.xml file. The Code is as follows:

1 <RelativeLayout xmlns: 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/users" 7 android: paddingRight = "@ dimen/activity_horizontal_margin" 8 android: paddingTop = "@ dimen/activity_vertical_margin" 9 tools: context = "com. example. gettxtdata. mainActivity $ PlaceholderFragment "> 10 11 <LinearLayout 12 android: layout_width =" fill_parent "13 android: layout_height =" fill_parent "14 android: orientation = "vertical" 15> 16 <EditText 17 android: id = "@ + id/inputText" 18 android: layout_width = "fill_parent" 19 android: layout_height = "wrap_content" 20 android: text = "http://www.sohu.com" 21/> 22 <Button 23 android: layout_width = "wrap_content" 24 android: layout_height = "wrap_content" 25 android: id = "@ + id/getTxtBtn" 26 android: text = "get text" 27/> 28 <ScrollView 29 android: layout_width = "fill_parent" 30 android: layout_height = "fill_parent" 31> 32 <TextView android: layout_width = "fill_parent" 33 android: layout_height = "wrap_content" 34 android: id = "@ + id/sohu" 35/> 36 </ScrollView> 37 </LinearLayout> 38 39 40 </RelativeLayout>View Code

3. Create a new class named NetTool. java, which is mainly for text operations. The method added to the class is as follows:

1 public static String getHtml (String path, String enconding) throws Exception {2 // creates a url Class Object and throws an Exception. 3 URL url = new URL (path ); 4 // obtain the HttpURLConnection object 5 HttpURLConnection conn = (HttpURLConnection) url. openConnection (); 6 conn. setRequestMethod ("GET"); // declares the Request Method 7 conn. setConnectTimeout (6*1000); // set the link timeout to 8 9 if (conn. getResponseCode () = 200) 10 {11 InputStream iStream = conn. getInputStream (); // get the input stream 12 byte [] data = readInstream (iStream); // get the data 13 14 // convert the byte to the String 15 return new String (data, enconding); 16}View Code

4. Modify the MainActivity. java code. The Code is as follows:

1 public class MainActivity extends ActionBarActivity {2 3 @ Override 4 protected void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. activity_main); 7 8 if (savedInstanceState = null) {9 getSupportFragmentManager (). beginTransaction () 10. add (R. id. container, new PlaceholderFragment ()). commit (); 11} 12} 13 14 @ Override15 public boolean onCreateOptionsMenu (Menu menu) {16 17 // Inflate the menu; this adds items to the action bar if it is present.18 getMenuInflater (). inflate (R. menu. main, menu); 19 return true; 20} 21 22 @ Override23 public boolean onOptionsItemSelected (MenuItem item) {24 // Handle action bar item clicks here. the action bar will25 // automatically handle clicks on the Home/Up button, so long26 // as you specify a parent activity in AndroidManifest. xml.27 int id = item. getItemId (); 28 if (id = R. id. action_settings) {29 return true; 30} 31 return super. onOptionsItemSelected (item); 32} 33 34/** 35 * A placeholder fragment containing a simple view.36 */37 @ TargetApi (Build. 38) public class PlaceholderFragment extends Fragment {39 40 public PlaceholderFragment () {41} 42 43 @ Override44 public View onCreateView (LayoutInflater inflater, ViewGroup container, 45 Bundle savedInstanceState) {46 View rootView = inflater. inflate (R. layout. fragment_main, container, 47 false); 48 StrictMode. setThreadPolicy (new 49 StrictMode. threadPolicy. builder (). detectDiskReads (). detectDiskWrites (). detectNetwork (). penaltyLog (). build (); 50 StrictMode. setVmPolicy (51 new StrictMode. vmPolicy. builder (). detectLeakedSqlLiteObjects (). detectLeakedClosableObjects (). penaltyLog (). penaltyDeath (). build (); 52 53 54 final EditText inputText = (EditText) rootView. findViewById (R. id. inputText); 55 final Button getBtn = (Button) rootView. findViewById (R. id. getTxtBtn); 56 final TextView txtView = (TextView) rootView. findViewById (R. id. sohu); 57 58 getBtn. setOnClickListener (new View. onClickListener () {59 60 @ Override61 public void onClick (View v) {62 // TODO Auto-generated method stub63 String path = inputText. getText (). toString (); 64 try65 {66 String html = NetTool. getHtml (path, "GBK"); 67 txtView. setText (html); 68 69 Toast. makeText (MainActivity. this, 70 "succeeded in obtaining text", Toast. LENGTH_LONG ). show (); 71} catch (Exception e) 72 {73 Toast. makeText (MainActivity. this, 74 "failed to get text", Toast. LENGTH_LONG ). show (); 75 e. printStackTrace (); 76} 77} 78}); 79 return rootView; 80} 81} 82 83}View Code

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

 

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.