类中的内容
package com.example.helper.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteOrder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
/**
* 封装的 网络 + 线程
*/
public class HttpUtils {
// 使用线程池来下载图片,同一时刻,最多有3个线程在运行
private static ExecutorService execuotrs = Executors.newFixedThreadPool(3);
interface OnBitmapNetWorkResponse {
public void ok(Bitmap bitmap);
public void error(String error);
}
public static void RequestBitmapNetWork(final String path,
final OnBitmapNetWorkResponse response) {
final Handler handler = new Handler();
execuotrs.execute(new Runnable() {
@Override
public void run() {
boolean isNetWorkOK = false;
try {
URL url = new URL(path);
HttpURLConnection openConnection = (HttpURLConnection) url
.openConnection();
openConnection.setConnectTimeout(5000);
openConnection.connect();
if (openConnection.getResponseCode() == 200) {
InputStream inputStream = openConnection
.getInputStream();
final Bitmap bitmap = BitmapFactory
.decodeStream(inputStream);
handler.post(new Runnable() {
@Override
public void run() {
response.ok(bitmap);
}
});
inputStream.close();
isNetWorkOK = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (!isNetWorkOK) {
handler.post(new Runnable() {
@Override
public void run() {
response.error("服务器不在服务区内!");
}
});
}
}
}
});
}
public interface OnNetWorkResponse {
public void ok(String response);
public void error(String error);
}
public static void RequestNetWork(final String path,
final OnNetWorkResponse response) {
//实例化handler
final Handler hanlder = new Handler();
new Thread() {
public void run() {
//标志位
boolean isWorkOK = false;
InputStream inputStream = null;
ByteArrayOutputStream outStream = null;
try {
URL url = new URL(path);
System.out.println("=======path========="+path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(10000);
//允许输入流,即允许下载
connection.setDoInput(true);
//上面的配置必须在connect之前完成
connection.connect();
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
outStream = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
outStream.write(b, 0, len);
}
outStream.flush();
final String result = new String(
outStream.toByteArray());
hanlder.post(new Runnable() {
@Override
public void run() {
System.out.println("得到的结果是"+result);
response.ok(result);
}
});
isWorkOK = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 网络操作出问题
if (!isWorkOK) {
response.error("服务器走神拉!");
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
}.start();
}
}
关于文件读取操作,可以再看下这个例子
package lgx.wea.myweather;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpDownloader {
// 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文本当中的内容
// 1.创建一个URL对象
// 2.通过URL对象,创建一个HttpURLConnection对象
// 3.得到InputStream
// 4.从InputStream当中读取数据
private URL url;
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
System.out.println("这个是尝试输出的");
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
System.out.println("看下HttpURLConnection是否为空"+urlConn.getInputStream());
buffer = new BufferedReader(new InputStreamReader(
urlConn.getInputStream()));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
urlConn.disconnect() ;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(buffer!=null){
buffer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
利用该类的小方法,请看这里http://blog.csdn.net/jack_king007/article/details/41174425