Android Tool Code --- HttpDownloader from URL

原创 2015年07月07日 16:45:38
package com.download;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

import mars.utils.FileUtils;


public class HttpDownloader {
	private URL url = null;
	private final static String TAG = "MP_DownLoad";	
	/**
	 * @param urlStr
	 * @return
	 */
	public String download(String urlStr) {
		StringBuffer 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 {
				buffer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 */
	public int downFile(String urlStr, String path, String fileName) {
		InputStream inputStream = null;
		try {
			FileUtils fileUtils = new FileUtils();
			if (fileUtils.isFileExist(fileName,path)) {
				return 1;
			} else {
				inputStream = getInputStreamFromUrl(urlStr);
				File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);
				if (resultFile == null) {
					return -1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			try {
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	/**
	 * 
	 * @param urlStr
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public InputStream getInputStreamFromUrl(String urlStr)
			throws MalformedURLException, IOException {
		url = new URL(urlStr);
		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
		InputStream inputStream = urlConn.getInputStream();
		return inputStream;
	}
}


Test Code:

	private String downloadXML(String urlStr) {
		HttpDownloader httpDownloader = new HttpDownloader();
		String result = httpDownloader.download(urlStr);
		return result;
	}

        String xml = downloadXML("http://192.168.42.124:8080/mp3/resources.xml");






版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

code review tool from google

  • 2008-10-06 14:49
  • 703KB
  • 下载

Build update.zip from your own android source code

本文介绍如何从一个已经编译好的android源码树中提取一个update.zip所需要的所有资源, 从而最终构建一个可以在android recovery模式下刷机的update.zip包.在介绍之前...

Learn From Android Source Code之异步消息处理线程

这里所说的异步消息处理线程不要和跨进程调用(IPC)混淆,看到Handler对象,就应该想到异步处理线程。 对于普通线程而言,执行完run()方法内的代码后线程就结束。而异步处理线程是指,线程启动后...

(转自Mars) Android - 文件下载类 HttpDownloader,内存卡操作类 FileUtils

HttpDownloader.java   1 package mars.utils; 2 3 import java.io.BufferedReader; 4 import java....
  • Baple
  • Baple
  • 2015-07-23 14:21
  • 822

CRM2011发布了用于验证自定义代码的工具(Custom Code Validation Tool)

由于Dynamics CRM 2011 R8(UR9) 中引入了跨浏览器的功能,在实现这项功能的过程中CRM开发团队引入了大量的代码修改, 对于之前的Javascript在CRM中运行的机制,为了向...

QR-Image-from-URL_v1.2.crx

  • 2017-09-06 09:23
  • 42KB
  • 下载

create toolbar when Excel VBA Open (extract from tool 1)

********** thisworkbook ********** Option Explicit Private Const gs_toolbar_extract_rpt_a As Str...

linux code format tool indent

indent.sh ------------------------------------------------ for file in `find ./ -name "*.c"`; do ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)