When the android handler starts to download text content from the network, the following prompt is displayed: progressbar-circular progress bar.

Source: Internet
Author: User

After reading a lot of examples of downloading text, I thought it was not easy to understand. I wrote a simple and easy-to-understand code.

Package COM. example. demodownload; import android. app. activity; import android. app. progressdialog; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private button; private textview show; private string STR; private progressdialog mypdialog; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); button = (button) findviewbyid (R. id. button1); show = (textview) findviewbyid (R. id. textview1); button. setonclicklistener (New onclicklistener () {public void onclick (view v) {downloadthread thread = new downloadthread (); thread. start ();} }); Mypdialog = new progressdialog (this); // instantiate mypdialog. setprogressstyle (progressdialog. style_spinner); // you can specify the progress bar style. The style is circular and the mypdialog is rotated. settitle ("login"); // set progressdialog title mypdialog. setmessage ("downloading... please wait"); // sets the progressdialog prompt message mypdialog. setindeterminate (false); // sets progressdialogmypdialog. setcancelable (true); // set progressdialog // whether the ssdialog can be canceled by pressing the return button} handler = new handler () {@ overridepublic void Han Dlemessage (Message MSG) {Switch (MSG. what) {Case 1: // start the download mark mypdialog. show (); // Let progressdialog display break; Case 2: // The end download mark mypdialog. cancel (); show. settext (STR); break ;}}; private class downloadthread extends thread {@ overridepublic void run () {message MSG = new message (); MSG. what = 1; // start the download mark 1handler. sendmessage (MSG); httpdownloader HTTP = new httpdownloader (); STR = http. download ("http://m.weather.com. CN/data/101010100.html "); MSG = new message (); // This is required; otherwise, an error is returned. Because MSG. What is generally final type MSG. What = 2; handler. sendmessage (MSG );}}}

The layout file is not pasted, mainly a button and a button. Paste the Download text class

Package COM. example. demodownload; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import java.net. httpurlconnection; import java.net. URL; public class httpdownloader {// download the file based on the URL, provided that the content in the file is text, and the return value of the function is the content in the text. // 1. create a URL object // 2. create an httpurlconnection object through a URL object // 3. obtain inputstream // 4. read Private URL from inputstream; Public String download (string urlstr) {strin Gbuffer sb = new stringbuffer (); string line = NULL; bufferedreader buffer = NULL; try {url = new URL (urlstr); httpurlconnection urlconn = (httpurlconnection) URL. openconnection (); buffer = new bufferedreader (New inputstreamreader (urlconn. getinputstream (); While (line = buffer. readline ())! = NULL) {sb. append (line) ;}} catch (exception e) {e. printstacktrace () ;}finally {try {If (buffer! = NULL) {buffer. close () ;}else {return "error" ;}} catch (ioexception e) {e. printstacktrace () ;}} return sb. tostring ();}}

By the way, do not forget to configure network permissions.

What surprised me is that the manifest. xml file is written correctly.

    <uses-sdk android:minSdkVersion="8" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.INTERNET" />

If it is written as below, an error will be reported because the buffer is empty during the download. If you have searched the internet for a long time and do not know what is going on, which of the following knows what is going on, you can leave a message. Thank you first.

  <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.INTERNET" />

Now, when you download a file or parse network resources, you can give the user a friendly interactive interface to wait. Of course, you can set the download process. This progressbar is just a prompt, there is no progress. You can use another progressbar to kindly prompt the download progress. I will post it later.

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.