设置URL从parse.com下载文件 [英] Setting url to download files from parse.com
本文介绍了设置URL从parse.com下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个下载开始按钮click.The按钮列表视图的singleitemvew活动present。我看到下载文件的示例分配一个URL像这样
私有静态字符串FILE_URL =HTTP://一些示例网址
我用解析下载文件。我该如何分配值FILE_URL
这里是我的code
singleitemvew.java
公共类SingleItemView扩展活动 {
ProgressDialog mProgressDialog;
字符串标题;
串副标题;
串levelImg;
串apiImg;
串apiText;
串descImgOne;
串descImgTwo;
字符串codeTEXT;
ImageLoader的imgloader =新ImageLoader的(本);串fileurl;私人ProgressDialog pDialog;
ImageView的my_image;
//进度对话框类型(0 - 对于水平进度条)
公共静态最终诠释progress_bar_type = 0;
@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.singleitemview); 意向I = getIntent(); 标题= i.getStringExtra(标题);
副标题= i.getStringExtra(副标题);
levelImg = i.getStringExtra(levelimg);
apiImg = i.getStringExtra(apiimg);
apiText = i.getStringExtra(apitext);
descImgOne = i.getStringExtra(descimgone);
descImgTwo = i.getStringExtra(descimgtwo);
codeTEXT = i.getStringExtra(codeTEXT);
fileurl = i.getStringExtra(下载); TextView的headingtxt =(的TextView)findViewById(R.id.singleitemheading);
TextView的subheadingtxt =(的TextView)findViewById(R.id.singleitemsubheading);
TextView的apitxt =(的TextView)findViewById(R.id.singleitemviewapitext);
TextView的codetxt =(的TextView)findViewById(R.id.singleitemview codeTEXT); ImageView的水平=(ImageView的)findViewById(R.id.levelimg);
ImageView的API =(ImageView的)findViewById(R.id.singleitemviewapiimg);
ImageView的descone =(ImageView的)findViewById(R.id.descriptionimgone);
ImageView的desctwo =(ImageView的)findViewById(R.id.descriptionimgtwo); 按钮downloadbtn =(按钮)findViewById(R.id.singleitemviewButton); headingtxt.setText(航向);
subheadingtxt.setText(小标题);
apitxt.setText(apiText);
codetxt.setText(codeTEXT); imgloader.DisplayImage(levelImg,水平);
imgloader.DisplayImage(apiImg,API);
imgloader.DisplayImage(descImgOne,descone);
imgloader.DisplayImage(descImgTwo,desctwo); downloadbtn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){ 新DownloadFileFromURL()执行(FILE_URL);
}
});}@覆盖
保护对话框onCreateDialog(INT ID){
开关(ID){
案例progress_bar_type://我们设置它为0
pDialog =新ProgressDialog(本);
pDialog.setMessage(下载文件,请稍候...);
pDialog.setIndeterminate(假);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(真);
pDialog.show();
返回pDialog;
默认:
返回null;
}
}类DownloadFileFromURL扩展的AsyncTask<字符串,字符串,字符串> { / **
*启动后台线程之前
*显示进度条对话框
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
的ShowDialog(progress_bar_type);
} / **
在后台线程下载*文件
* * /
@覆盖
保护字符串doInBackground(字符串... f_url){
诠释计数;
尝试{ 网址URL =新的URL(f_url [0]); URLConnection的连接如= url.openConnection();
conection.connect();
//这将是有益的,这样就可以显示tipical 0-100%的进度条
INT lenghtOfFile = conection.getContentLength(); //下载文件
InputStream的输入=新的BufferedInputStream(url.openStream(),8192); //输出流
OutputStream的输出=新的FileOutputStream(/ SD卡/下载/ downloadedfile.rar); 字节的数据[] =新的字节[1024]; 总长= 0; 而((计数= input.read(数据))!= - 1){
总+ =计数;
//发布进度....
//在此之后onProgressUpdate将被称为
publishProgress(+(int)的((总* 100)/ lenghtOfFile)); //将数据写入到文件
output.write(数据0,计);
} //冲洗输出
output.flush(); //关闭流
output.close();
input.close(); }赶上(例外五){
Log.e(错误,e.getMessage());
} 返回null;
}/ **
*更新进度条
* * /
保护无效onProgressUpdate(字符串...进度){
//设置进度百分比
pDialog.setProgress(的Integer.parseInt(进展[0]));
}/ **
*在完成后台任务之后
*辞退进度对话框
* ** /
@覆盖
保护无效onPostExecute(字符串FILE_URL){
//关闭该对话框的文件下载后
dismissDialog(progress_bar_type);
}}
}
解决方案
1.Edit您DownloadFileFromURL类是这样的。
类DownloadFileFromURL扩展的AsyncTask<字符串,太虚字符串>
{
串FILE_URL;
公共DownloadFileFromURL(字符串FILE_URL)
{
this.file_url = FILE_URL;
}
//删除网址URL =新的URL(f_url [0]);从doInBackground()方法
}
2.change SingleItemViewClass的调用对象
downloadbtn.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
新DownloadFileFromURL(FILE_URL).execute();
}
});
让我知道,如果一些问题。
I require a download to start on button click.The button is present in a singleitemvew activity of list view. The example I saw to download files assigns a single url like this
private static String file_url = " http:// some example url"
I use parse to download files. How do I assign the value to "file_url" Here is my code
singleitemvew.java
public class SingleItemView extends Activity
{
ProgressDialog mProgressDialog;
String heading;
String subHeading;
String levelImg;
String apiImg;
String apiText;
String descImgOne;
String descImgTwo;
String codeText;
ImageLoader imgloader = new ImageLoader(this);
String fileurl;
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
Intent i = getIntent();
heading= i.getStringExtra("heading");
subHeading = i.getStringExtra("subheading");
levelImg = i.getStringExtra("levelimg");
apiImg = i.getStringExtra("apiimg");
apiText = i.getStringExtra("apitext");
descImgOne = i.getStringExtra("descimgone");
descImgTwo = i.getStringExtra("descimgtwo");
codeText = i.getStringExtra("codetext");
fileurl = i.getStringExtra("download");
TextView headingtxt = (TextView)findViewById(R.id.singleitemheading);
TextView subheadingtxt = (TextView)findViewById(R.id.singleitemsubheading);
TextView apitxt = (TextView)findViewById(R.id.singleitemviewapitext);
TextView codetxt = (TextView) findViewById(R.id.singleitemviewcodetext);
ImageView level =(ImageView) findViewById(R.id.levelimg);
ImageView api =(ImageView) findViewById(R.id.singleitemviewapiimg);
ImageView descone =(ImageView) findViewById(R.id.descriptionimgone);
ImageView desctwo =(ImageView) findViewById(R.id.descriptionimgtwo);
Button downloadbtn = (Button) findViewById(R.id.singleitemviewButton);
headingtxt.setText(heading);
subheadingtxt.setText(subHeading);
apitxt.setText(apiText);
codetxt.setText(codeText);
imgloader.DisplayImage(levelImg, level);
imgloader.DisplayImage(apiImg, api);
imgloader.DisplayImage(descImgOne, descone);
imgloader.DisplayImage(descImgTwo, desctwo);
downloadbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
new DownloadFileFromURL().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try{
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/download/downloadedfile.rar");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
}
解决方案
1.Edit your DownloadFileFromURL class like this.
class DownloadFileFromURL extends AsyncTask<String, Void String>
{
String file_url;
public DownloadFileFromURL(String file_url)
{
this.file_url = file_url;
}
//remove URL url = new URL(f_url[0]); from your doInBackground() method
}
2.change the calling Object of SingleItemViewClass
downloadbtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
new DownloadFileFromURL(file_url).execute();
}
});
Let me know if some Issue.
这篇关于设置URL从parse.com下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文