📄 httpthread.java
字号:
/**
*
* @author Sam Huang
*
*/
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
/**
*
* 下载图片的线程类
*
*/
public class HttpThread extends Thread {
private static final int MAX_LENGTH = 30 * 1024; // 30K
private boolean userInterrupted = false;
private String url;
private byte[] buffer = null;
private IHttpListener listener;
public HttpThread(String url, IHttpListener listener) {
this.url = url;
this.listener = listener;
}
public void setUserInterrupted() {
this.userInterrupted = true;
}
public void run() {
getDataFromUrl(url);
}
private void getDataFromUrl(String url) {
InputStream is = null;
HttpConnection c = null;
try {
listener.onProgress(0);
// 使用代理
//System.out.println(ImageAlbum.getConnectionMethod());
if (ImageAlbum.getConnectionMethod().equals("cmwap")) {
c = (HttpConnection) Connector.open(url);
c.setRequestProperty("X-Online-Host", "www.the3gwireless.com");
} else {
// 不使用代理
StringBuffer sb = new StringBuffer(url.substring(0, url
.indexOf("10.0.0.172")));
sb.append("www.the3gwireless.com");
sb.append(url.substring(sb.length()
- "www.the3gwireless.com".length()
+ "10.0.0.172".length(), url.length()));
url = sb.toString();
c = (HttpConnection) Connector.open(url);
}
// 默认即为GET
c.setRequestMethod(HttpConnection.GET);
// 获得响应码, 200表示成功
int code = c.getResponseCode();
if (code > HttpConnection.HTTP_OK) {
listener.onError(code, c.getResponseMessage());
return;
}
// 返回响应大小,或者-1如果大小无法确定,我是在WTK上测试通过的,每次都返回正确的尺寸,因此返回-1的代码我不知道是否正确,如有错请帮助改正
int size = (int) c.getLength();
int leftSize = 0;
int loopCount = 0;
if (size != -1) {
leftSize = size;
listener.onSetSize(size);
} else {
// 不知道大小,只好什么也不做,
}
// 开始读响应:
is = c.openInputStream();
int percent = 0; // 百分比
int tmp_percent = 0;
int index = 0; // 缓冲区索引
int reads; // 读取的字节数
if (size != -1) {
// 响应大小已知,确定缓冲区大小
buffer = new byte[size];
} else {// 响应大小未知,设定一个固定大小的缓冲区
buffer = new byte[MAX_LENGTH];
}
while (!userInterrupted) {
int len = buffer.length - index;
// 每次读取128个字节
len = len > 128 ? 128 : len;
try {
reads = is.read(buffer, index, len);
} catch (Exception e) {
break;
}
// reads<0表示已经读完所有数据,退出循环
if (reads <= 0)
break;
index += reads;
// 更新进度
if (size != -1) {
leftSize = size - index;
tmp_percent = index * 100 / size;
if (tmp_percent != percent) {
percent = tmp_percent;
listener.onProgress(percent);
}
// 设置已经读取的尺寸
listener.onSetSize(leftSize);
} else {
// 由于尺寸未知,所以是假的进度条,不过至少表示正在下载数据
percent = loopCount++ / 3;
listener.onProgress(percent);
}
}
if (!userInterrupted && is.available() > 0) // 缓冲区已满,无法继续读取
listener.onError(-1, "缓冲区溢出");
if (!userInterrupted) {
if (size != -1 && index != size)
listener.onError(-2, "内容不匹配");
else
listener.onFinish(buffer, index);
}
} catch (IOException ioe) {
listener.onError(-3, "发生 IOException" + ioe.getMessage());
} finally { // 清理资源
if (is != null)
try {
is.close();
} catch (IOException ioe) {
}
if (c != null)
try {
c.close();
} catch (IOException ioe) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -