標籤:
1、沒有實現伺服器端,為網上的一個下載連結。
2、網路開發不要忘記在設定檔中添加訪問網路的許可權
<uses-permission android:name="android.permission.INTERNET"/>
3、網路請求、處理不能在主線程中進行,一定要在子線程中進行。因為網路請求一般有1~3秒左右的延時,在主線程中進行造成主線程的停頓,對使用者體驗來說是致命的。(主線程應該只進行UI繪製,像網路請求、資源下載、各種耗時操作都應該放到子線程中)。
4、斷點下載返回碼為206,而不是200,斷點請求失敗的返回碼為416。
5、
/** * 斷點下載 */public class MoreTimesActivity extends Activity { private TextView mTvMsg; private String result = ""; private long start = 0; private long stop = 1024 * 1024; private int times = 0; // 根據檔案大小自己設的, @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_times_download); initView(); } private void initView(){ mTvMsg = (TextView) findViewById(R.id.tv_msg); new Thread(moreThread).start(); } private Thread moreThread = new Thread(){ public void run() { HttpURLConnection connection = null; try { URL url = new URL("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); // 設定開始下載的位置和結束下載的位置,單位為位元組 connection.setRequestProperty("Range", "bytes=" + start + "-" + stop); String path = Environment.getExternalStorageDirectory().getPath() + "/aaaaa/baidu_map.apk"; // 斷點下載使用的檔案對象RandomAccessFile RandomAccessFile access = new RandomAccessFile(path, "rw"); // 移動指標到開始位置 access.seek(start); InputStream is = null; Log.e("ADB----", connection.getResponseCode() + ""); if(connection.getResponseCode() == 206){ is = connection.getInputStream(); int count = 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 Message msg = Message.obtain(); msg.what = 0; result += "檔案" + times + "下載成功" + ":" + start + "---" + stop + "\n"; moreHandler.sendMessage(msg); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(connection != null){ connection.disconnect(); } } }; }; private Handler moreHandler = new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what == 0 && result!=null){ if(times >= 10){ Message msg1 = Message.obtain(); msg1.what = 1; moreHandler.sendMessage(msg1); }else{ new Thread(moreThread).start(); times += 1; } mTvMsg.setText(result); }else if(msg.what == 1){ mTvMsg.setText(result); } }; };}
6、斷點下載重要的是實現一:設定斷點請求setRequestProperty("Range", "bytes=0-1024");
二:通過RandomAccessFile來將下載的位元組插入到指定的位置。
7、如何?多線程斷點下載這裡不再介紹,大家可以自己思考一下。
8、參考博文: 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
android網路編程之HttpUrlConnection的講解--實現檔案斷點下載