android 網絡程序下載,Android之網絡文件下載

環境搭建:

本地服務器(下載wamp【Windows, Apache, MySQL, PHP 】,安裝即可,然后將網頁或文件放進 www directory)

虛擬機(訪問本地服務器的ip:10.0.2.2)

注意事項:

權限相關

網絡訪問 存儲卡訪問

文件權限 如:chmod? 777 /data

源代碼:

package com.example.net_ex04;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Environment;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.ProgressBar;

public class MainActivity extends Activity {

ProgressBar progressBar;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

progressBar = (ProgressBar)this.findViewById(R.id.progressBar1);

Button button = (Button)this.findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {

String url = "http://10.0.2.2/Music.flv";

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

progressBar.setVisibility(View.VISIBLE);

progressBar.setProgress(0);

new DownloadTask().execute(url);

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

class DownloadTask extends AsyncTask{

public DownloadTask() {

// TODO Auto-generated constructor stub

}

@Override

protected String doInBackground(String... params) {

// TODO Auto-generated method stub

HttpGet httpRequest = new HttpGet(params[0]);

HttpClient httpClient = new DefaultHttpClient();

try{

HttpResponse httpResponse = httpClient.execute(httpRequest);

HttpEntity entity = httpResponse.getEntity();

long length = entity.getContentLength();

progressBar.setMax((int)length);

InputStream inputStream = entity.getContent();

byte[] b = new byte[1024];

int readedLength = -1;

//File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "Music.flv");

File file = new File("/data/Music.flv");

if(!file.exists())

{

file.createNewFile();

}

OutputStream outputStream = new FileOutputStream(file);

int count = 0;

while((readedLength = inputStream.read(b)) != -1){

outputStream.write(b, 0, readedLength);

count += readedLength;

publishProgress(count);

}

}catch(ClientProtocolException e){

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}

return null;

}

protected void onProgressUpdate(Integer... values){

progressBar.setProgress(values[0]);

super.onProgressUpdate(values);

}

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/543011.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/543011.shtml
英文地址,請注明出處:http://en.pswp.cn/news/543011.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

FMDB的介紹

2019獨角獸企業重金招聘Python工程師標準>>> FMDB方法的介紹 1.首先我們需要創建一個FMDatabase實例: (FMDatabase*)DataBaseSigonInstance { //數據庫初始化 NSString *homeDir NSHomeDirectory(); //NSLog("%",homeDir); NSString *dbPath …

python 打印列表元素_Python程序以不同方式打印列表元素

python 打印列表元素In this program – we are going to learn how can we print all list elements, print specific elements, print a range of the elements, print list multiple times (using * operator), print multiple lists by concatenating them, etc. 在此程序中…

網絡克隆軟件_網文生成器,克隆的是騙錢“病毒”

文章克隆器頁面。圖據北京晚報如今不論男女老少,多半喜歡用手機收集信息、瀏覽自己關注的話題。有的時候,人們會發現,不少親朋發來的鏈接或者公眾號推送的文章,長得特別像,但多少有那么些微不同。其實,不是…

c#讀取指定字符后的字符_在C#中讀取字符的不同方法

c#讀取指定字符后的字符As we know that, Console.ReadLine() is used for input in C#, it actually reads a string and then we convert or parse it to targeted type. 眾所周知, Console.ReadLine()用于C#中的輸入,它實際上是讀取一個字…

使用python 對圖片進行水印,保護自己寫的文章

1,關于文章被爬 說起來挺桑心的,好不容易寫的文章,被爬走。 用個搜索引擎搜索都不是在第一位,寫的文章全給這些網站提供流量了。 這種網站還居多廣告。 還是抱怨少點吧。csdn對于這些事情也是無所作為啊。 最起碼的防盜鏈也不…

r語言descstats_一條命令輕松繪制CNS頂級配圖-ggpubr

Hadley Wickham創建的可視化包ggplot2可以流暢地進行優美的可視化,但是如果要通過ggplot2定制一套圖形,尤其是適用于雜志期刊等出版物的圖形,對于那些沒有深入了解ggplot2的人來說就有點困難了,ggplot2的部分語法是很晦澀的。為此…

android layout_width 屬性,android:layout_weight屬性詳解

在android開發中LinearLayout很常用,LinearLayout的內控件的android:layout_weight在某些場景顯得非常重要,比如我們需要按比例顯示。android并沒用提供table這樣的控件,雖然有TableLayout,但是它并非是我們想象中的像html里面的t…

angular的$http發送post,get請求無法傳送參數的問題

2019獨角獸企業重金招聘Python工程師標準>>> 用$http進行異步請求的時候發現了一個奇怪的事情,用$http.post(url,data)的方法進行請求,后臺死活接收不到data的參數,真是百思不得姐啊..... 折騰了老半天才在stackoverflow上找到答案…

python變量和常量_Python數學模塊常量和示例

python變量和常量Python數學模塊常量 (Python math module constants) In the math module, there are some of the defined constants that can be used for various mathematical operations, these are the mathematical constants and returns their values equivalent to …

怎樣解決Word文檔圖標無法正常顯示的問題?

此類問題是由于 Word 程序相關組件損壞導致,可以通過下面的方案來解決:步驟/方法按鍵盤上的 Windows 徽標健 R 鍵,輸入 regedit,按回車鍵。(若彈出用戶賬戶控制窗口,請允許以繼續)對于 Word 200…

android 對話框的父view是誰,android – 在對話框中獲取相對于其父級的視圖位置

我想要做的是,從按鈕邊緣到屏幕上的一點畫一條線……我正在使用一個對話框片段…我嘗試的所有函數總是返回0點…我試過以下:Overrideprotected Dialog createDialog(Bundle savedInstanceState, FragmentActivity activity){Dialog d builder.create();View v Lay…

np.radians_帶有Python示例的math.radians()方法

np.radiansPython math.radians()方法 (Python math.radians() method) math.radians() method is a library method of math module, it is used to convert angle value from degree to radians, it accepts a number and returns the angle value in radians. math.radians(…

怎么用git將本地代碼上傳到遠程服務器_git之如何把本地文件上傳到遠程倉庫的指定位置...

2018.11.26添加內容:對于自己的倉庫,我們建議將遠程倉庫通過clone命令把整個倉庫克隆到本地的某一路徑下。這樣的話我們從本地向遠程倉庫提交代碼時,就可以直接把需要提交的文件拖到我們之前克隆下來的路徑下,接下來在這整個倉庫下…

MathType與Origin是怎么兼容的

MathType作為一款常用的公式編輯器,可以與很多的軟件兼容使用。Origin雖然是一款專業繪圖與數據分析軟件,但是在使用過程中也是可以用到MathType。它可以幫助Origin給圖表加上標簽,或者在表格中增加公式標簽。但是一些用戶朋友對這個不能不是…

c語言 函數的參數傳遞示例_llround()函數以及C ++中的示例

c語言 函數的參數傳遞示例C llround()函數 (C llround() function) llround() function is a library function of cmath header, it is used to round the given value and casts to a long long integer, it accepts a number and returns the integer (long long int) valu…

android requestmtu,android - 如何設置/獲取/請求從Android到iOS或反之亦然BLE的MTU? - 堆棧內存溢出...

我們正在將MTU請求從Android發送到iOSAndroid-從此函數onServicesDiscovered回調請求MTU但是我不知道如何確定對等設備支持是否請求了MTU,以及如何實際協商的MTU。 僅在API級別22(Android L 5.1)中添加了必需的函數:BluetoothGattCallback.onMtuChanged(…

AutoBookmark Adobe Acrobat快速自動批量添加書簽/目錄

前言 解決問題:Adobe Acrobat快速自動批量添加書簽/目錄, 徹底告別手動添加書簽的煩惱 AutoBookmark 前言1 功能簡介2 實現步驟2.1 下載插件2.2 將插件復制到Acrobat文件夾下2.3 自動生成書簽 1 功能簡介 我們在查看PDF版本的論文或者其他文件的時候, 雖然相比較于…

Python調用微博API獲取微博內容

一:獲取app-key 和 app-secret 使用自己的微博賬號登錄微博開放平臺(http://open.weibo.com/),在微博開放中心下“創建應用”創建一個應用,應用信息那些隨便填,填寫完畢后,不需要提交審核,需要的只是那個ap…

python獨立log示例_帶有Python示例的math.log1p()方法

python獨立log示例Python math.log1p()方法 (Python math.log1p() method) math.log1p() method is a library method of math module, it is used to get the natural logarithm of 1x (base e), it accepts a number and returns the natural logarithm of 1number on base e…

15947884 oracle_Oracle Patch Bundle Update

一、相關知識介紹以前只知道有CPU(Critical Patch Update)和PSU(Patch Set Update),不知道還有個Bundle Patch,由于出現了TNS-12531的BUG問題,需要在windows上打至少為Patch bundle 22補丁。通過學習查找:Oracle里的補丁具體分為如下這樣6種類型&#xf…