1, does not realize the server side, for the online download link.
2, the network development do not forget to add access to the network in the configuration file permissions
<uses-permission android:name= "Android.permission.INTERNET"/>
3, network requests, processing can not be carried out in the main thread, must be in the sub-thread. Because the network request generally has about 1-3 seconds of delay, in the main thread to cause the main thread of the pause, the user experience is fatal. (The main thread should be only UI-drawn, such as network requests, resource downloads, and various time-consuming operations should be placed in child threads).
4, Breakpoint download return code is 206, instead of 200, breakpoint request failed return code is 416.
5.
/*** Breakpoint Download*/ Public classMoretimesactivityextendsActivity {PrivateTextView mtvmsg; PrivateString result = ""; Private LongStart = 0; Private LongStop = 1024 * 1024; Private intTimes = 0;//according to the size of the file you set,@Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_times_download); Initview (); } Private voidInitview () {mtvmsg=(TextView) Findviewbyid (r.id.tv_msg); NewThread (Morethread). Start (); } PrivateThread Morethread =NewThread () { Public voidrun () {httpurlconnection connection=NULL; Try{URL URL=NewURL ("http://ftp-apk.pconline.com.cn/ef19af4e28462271af1117efaf868bc2/pub/download/201010/renshengrili_v4.0.04.05.apk"); Connection=(HttpURLConnection) url.openconnection (); Connection.setrequestmethod ("GET"); Connection.setdoinput (true); //set the location to start the download and where to end the download, in bytesConnection.setrequestproperty ("Range", "bytes=" + Start + "-" +stop); String Path= Environment.getexternalstoragedirectory (). GetPath () + "/aaaaa/baidu_map.apk"; //the file object used by the breakpoint download RandomaccessfileRandomaccessfile Access =NewRandomaccessfile (Path, "RW"); //move the pointer to the start positionAccess.seek (start); InputStream is=NULL; LOG.E ("ADB----", connection.getresponsecode () + ""); if(Connection.getresponsecode () = = 206) { is=Connection.getinputstream (); intCount = 0; byte[] buffer =New byte[1024]; while((count = is.read (buffer))! =-1) {access.write (buffer,0, Count); } } if(Access! =NULL) {access.close (); } if(Is! =NULL) {is.close (); } Start= Stop + 1; Stop+ = 1024*1024;//1M per downloadMessage msg=Message.obtain (); Msg.what= 0; Result+ = "File" + times + "Download Success" + ":" + start + "---" + stop + "\ n"; Morehandler.sendmessage (msg); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(Connection! =NULL) {connection.disconnect (); } } }; }; PrivateHandler Morehandler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {if(Msg.what = = 0 && result!=NULL){ if(Times >= 10) {Message MSG1=Message.obtain (); Msg1.what= 1; Morehandler.sendmessage (MSG1); }Else{ NewThread (Morethread). Start (); Times+ = 1; } mtvmsg.settext (Result); }Else if(Msg.what = = 1) {Mtvmsg.settext (result); } }; };}
6, Breakpoint download important is to implement a: Set breakpoint request Setrequestproperty ("Range", "bytes=0-1024");
Two: The downloaded bytes are inserted into the specified location by Randomaccessfile.
7, how to implement multi-threaded breakpoint download no longer introduced here, you can think for yourself.
8. Reference Blog: http://blog.sina.com.cn/s/blog_413580c20100wmr8.html
http://my.oschina.net/u/141149/blog/55337
Http://www.cnblogs.com/xyxiong/archive/2011/08/19/2145869.html
HttpURLConnection of Android Network programming--Implement file breakpoint download