📄 connectthread.java
字号:
/*
* connectThread.java
*
* Created on 2006年9月6日, 上午12:17
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.j2me.networld.connection;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
/**
*
* @author DONGHOME
*/
public class ConnectThread
implements CommandListener, Runnable
{
private NWC_MIDlet midlet = null;
private Form form = null;
private String url = null;
private HttpConnection hc = null;
private InputStream is = null;
private static final int LENGTH = 256;
private static final Command backCmd = new Command("Back", Command.BACK, 1);
private static final Command exitCmd = new Command("Exit", Command.EXIT, 0);
/** Creates a new instance of connectThread */
public ConnectThread(NWC_MIDlet midlet, String url)
{
this.midlet = midlet;
this.url = url;
form = new Form(this.url);
form.addCommand(backCmd);
form.addCommand(exitCmd);
form.setCommandListener(this);
}
public void run()
{
try
{
ProgressUI progress = new ProgressUI("Connecting networld...");
progress.init();
midlet.setCurrent(progress);
hc = (HttpConnection)Connector.open(this.url);
int code = hc.getResponseCode();
int connectLength = (int)hc.getLength();
if(code != HttpConnection.HTTP_OK)
{
form.append("Server is not responding!");
midlet.setCurrent(form);
return;
}
is = hc.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[LENGTH];
int length = 0;
int readLength = 0;
while((length = is.read(buffer, 0, buffer.length)) != -1)
{
readLength = readLength + length;
progress.onProgress(readLength * 100 / connectLength);
baos.write(buffer, 0, length);
}
byte[] data = baos.toByteArray();
String message = new String(data, 0, data.length);
form.append(message);
}
catch(Exception e)
{
form.append(new StringItem("Exception: ", e.toString()));
}
finally
{
if(is != null)
{
try
{
is.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
if(hc != null)
{
try
{
hc.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
midlet.setCurrent(form);
}
}
public void commandAction(Command cmd, Displayable dis)
{
if(cmd == backCmd)
{
midlet.setCurrent(new MainUI(this.midlet));
}
else if(cmd == exitCmd)
{
midlet.exitApp();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -