13,096
社区成员
发帖
与我相关
我的任务
分享
public class ExamTest extends MIDlet implements CommandListener {
public static final byte WAIT = 0;// 等待
public static final byte CONNECT = 1;// 连接中
public static final byte SUCCESS = 2;// 成功
public static final byte FAIL = 3;// 失败
int state;// 当前状态
Display display = Display.getDisplay(this);
Form form = new Form("HttpTest");
// StringBuffer sb = new StringBuffer("");
// StringItem si = new StringItem(null, sb.toString());
Command connect = new Command("联网", Command.OK, 1);
Command cls = new Command("清除网址", Command.OK, 1);
Command cancel = new Command("取消", Command.CANCEL, 1);
Command exit = new Command("退出", Command.EXIT, 1);
Command back = new Command("返回", Command.BACK, 1);
ChoiceGroup cmwapCG;// 接入方式
TextField urlTF;// 网址输入框
String url;// 网址
Http http;// 网络线程
Form page;// 页面
Alert waitPage;// 等待界面
public ExamTest() {
state = WAIT;// 等待状态
// 初始化等待界面
waitPage = new Alert("请等待", "网络连接中...", null, AlertType.INFO);
waitPage.addCommand(cancel);
waitPage.setCommandListener(this);
waitPage.setTimeout(Alert.FOREVER);
// 初始化页面
page = new Form("访问的页面");
page.addCommand(back);
page.setCommandListener(this);
// 初始化
cmwapCG = new ChoiceGroup("网络接入方式:", Choice.POPUP,
new String[] { "CMWAP" }, null);
urlTF = new TextField("请输入网址:",
"http://231599447.x6.fjjsp01.com/servlet/TestHttp", 100, TextField.URL);
form.append(cmwapCG);
form.append(urlTF);
// form.append(si);
form.addCommand(connect);
form.addCommand(cls);
// form.addCommand(cancel);
form.addCommand(exit);
form.setCommandListener(this);
}
protected void destroyApp(boolean b) {
}
protected void pauseApp() {
}
protected void startApp() {
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == connect) {// 联网
if (checkError())
return;
// display.setCurrent(waitPage);
http = new Http();
new Thread(http).start();
} else if (c == cancel) {// 取消
http.cancel();
http = null;
display.setCurrent(form);
} else if (c == back) {// 返回
page.deleteAll();
display.setCurrent(form);
} else if (c == cls) {// 清除数据
// cls();
urlTF.setString("");
} else if (c == exit) {// 退出
destroyApp(true);
notifyDestroyed();
}
}
// private void cls() {
// form.deleteAll();// 删除日志
// sb.delete(0, sb.length());
// si.setText(null);
// form.append(cmwapCG);
// form.append(urlTF);
// form.append(si);
// }
/**
* 判断是否正确
*
* @return
*/
private boolean checkError() {
url = urlTF.getString();
if (state == CONNECT) {
addStr("当前已有网络连接,请稍候");
return true;
} else if (url == null || url.trim().equals("")) {
addStr("url不能为空");
return true;
}
if (!url.toLowerCase().startsWith("http://")) {// 添加http://
url = "http://" + url;
}
System.out.println(url);
return false;
}
/**
* 添加文字
*
* @param str
* 要添加的文字
*/
private void addStr(String str) {
// sb.append(str + "\n");
// si.setText(sb.toString());
System.out.println(str);
}
/**
* 获取URL域名
*
* @param srcUrl
* @param domain
* true为截域名,false为截地址
* @return
*/
private String getURLhost(String srcUrl) {
int k = srcUrl.toLowerCase().indexOf("/", 7);
if (k == -1)
return srcUrl.substring(7);
return srcUrl.substring(7, k);
}
/**
*
* 字符串替换方法(字符串替换字符串)
*
* @author kf156
*
* @param text
* 文本
* @param oldStr
* 旧字符串
* @param newStr
* 新字符串
* @return
*/
public static String replace(String text, String oldStr, String newStr) {
int oldLen = oldStr.length();
int k = 0;
while (k + oldLen <= text.length()) {// 检测到字符串末尾
k = text.indexOf(oldStr, k);// 从k处查找oldStr,并返回位置
if (k == -1)// 若查不到,则跳出
return text;
// 若有,则将其替换为newStr
text = text.substring(0, k) + newStr
+ text.substring(k += oldLen, text.length());
}
return text;
}
class Http implements Runnable {
HttpConnection http;
boolean cancel;// 是否取消
public void cancel() {
cancel = true;
state = WAIT;// 等待状态
}
public void run() {
// cls();
state = CONNECT;
addStr("网络连接中...");
DataInputStream is = null;
try {
boolean cmwap = cmwapCG.isSelected(0);// 是否走CMWAP
//boolean cmwap = false;
String host = getURLhost(url);
System.out.println("host==="+host);
String _url = cmwap ? replace(url, host, "10.0.0.172:80") : url;
System.out.println("_url==="+_url);
http = (HttpConnection) Connector.open(_url, Connector.READ,
true);
if (cmwap)
http.setRequestProperty("X-Online-Host", host);
http.setRequestMethod(HttpConnection.GET);
cmwapCG.setSelectedIndex(0, true);
String contentType = http.getHeaderField("Content-Type");
// System.out.println(contentType);
addStr("Content-Type=" + contentType);
if (cancel) {// 中途结束
addStr("已取消");
return;
}
if (contentType != null
&& contentType.indexOf("text/vnd.wap.wml") != -1) {// 过滤移动资费页面
page.append("移动资费页面,过滤!");
http.close();
http = null;
http = (HttpConnection) Connector.open(_url,
Connector.READ, true);
if (cmwap)
http.setRequestProperty("X-Online-Host", host);
http.setRequestMethod(HttpConnection.GET);
contentType = http.getHeaderField("Content-Type");
addStr("Content-Type=" + contentType);
}
int code = http.getResponseCode();
addStr("HTTP Code :" + code);
if (code == 200) {// 成功
is = http.openDataInputStream();
byte[] b = new byte[1024];
int len = 0;
ExamGroup eg;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
page.append("1111");
byte num[] = new byte[1];
num[0] = is.readByte();
page.append("22222");
while ((len = is.read(b)) != -1) {
if (cancel) {// 中途结束
addStr("已取消");
return;
}
dos.write(b, 0, len);
}
page.append("3333");
byte[] data = baos.toByteArray();
eg = new ExamGroup(num,true);
page.append("num=="+num[0]);
eg.deserialize(data);
page.append("55555");
page.append(eg.getTimu()[8]);
page.append("6666");
data = null;
dos.close();
dos = null;
baos.close();
baos = null;
is.close();
is = null;
state = SUCCESS;
} else {
addStr("访问页面失败");
page.append("访问页面失败: " + code);
state = FAIL;
}
} catch (IOException e) {
addStr("联网发生异常:" + e.toString());
e.printStackTrace();
if (!cancel)
page.append("网络异常:" + e.toString());
state = FAIL;
} catch (SecurityException e) {
addStr("安全异常:" + e.toString());
e.printStackTrace();
if (!cancel)
page.append("安全异常:" + e.toString());
state = FAIL;
} catch (Exception e) {
addStr("发生异常:" + e.toString());
e.printStackTrace();
if (!cancel)
page.append("异常:" + e.toString());
state = FAIL;
} finally {
if (is != null) {// 关闭is
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is = null;
}
if (http != null) {// 关闭http
try {
http.close();
} catch (IOException e) {
e.printStackTrace();
}
http = null;
}
}
if (!cancel)
display.setCurrent(page);// 显示页面
}
}
}
public class ExamGroup {
private String timu[]={};//一个长达3000字的数组
private String cue[];
private byte daan[]={
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2};
private byte total ;
public ExamGroup(int bianhao){
}
public byte[] serialize() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(bout);
System.out.println("没错");
for(int i =0 ; i<total ; i++){
dataStream.writeUTF(timu[i]);
dataStream.writeByte(daan[i]);
}
System.out.println("serialize没错");
return bout.toByteArray();
}
public String[] getTimu() {
return timu;
}
public void setTimu(String[] timu) {
this.timu = timu;
}
public String[] getCue() {
return cue;
}
public void setCue(String[] cue) {
this.cue = cue;
}
public byte[] getDaan() {
return daan;
}
public void setDaan(byte[] daan) {
this.daan = daan;
}
public byte getTotal() {
if(timu.length>120){
total=120;
}else{
total = (byte)timu.length;
}
return total;
}
}
public class TestHttp extends HttpServlet {
public TestHttp() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/octet-stream");
getExamGroup(request,response,1);
}
public void getExamGroup(HttpServletRequest request, HttpServletResponse response,int bianhao ) throws IOException{
response.setContentType("application/octet-stream");
ExamGroup eg = new ExamGroup(bianhao);
OutputStream os = response.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeByte(eg.getTotal());
byte[] data = eg.serialize();
dos.write(data);
response.setContentLength(dos.size());
dos.close();
os.close();
}
}
public class ExamGroup {
private String timu[]={};//一个长达3000字的数组 数组内容省略
private String cue[];
private byte daan[]={
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2,
2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,2};
private byte total ;
public ExamGroup(int bianhao){
}
public byte[] serialize() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(bout);
System.out.println("没错");
for(int i =0 ; i<total ; i++){
dataStream.writeUTF(timu[i]);
dataStream.writeByte(daan[i]);
}
System.out.println("serialize没错");
return bout.toByteArray();
}
public String[] getTimu() {
return timu;
}
public void setTimu(String[] timu) {
this.timu = timu;
}
public String[] getCue() {
return cue;
}
public void setCue(String[] cue) {
this.cue = cue;
}
public byte[] getDaan() {
return daan;
}
public void setDaan(byte[] daan) {
this.daan = daan;
}
public byte getTotal() {
if(timu.length>120){
total=120;
}else{
total = (byte)timu.length;
}
return total;
}
}