我在j2me中编写了以下代码,但它总是抛出ConnectionNotFoundException。我已禁用Windows防火墙,以防它阻止netbeans网络活动。
package networking;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;
/**
* @author Kanishk
*/
public class Midlet extends MIDlet implements CommandListener{
private Command command;
private Form form;
private Display display;
private String url = "http://www.gzip.org/algorithm.txt";
private String local="http://127.0.0.1:8000";
private HttpConnection c = null;
private InputStream s = null;
private Connection conn;
public void startApp() {
display = Display.getDisplay(this);
form = new Form("Networking");
command = new Command("Exit",Command.EXIT,0);
form.addCommand(command);
try {
//conn = Connector.open(url);
c = (HttpConnection) Connector.open(url);
s = c.openInputStream();
byte[] byteArray = new byte[1024];
StringBuffer sb = new StringBuffer();
int readBytes = 0;
while ((readBytes = s.read(byteArray)) > 0) {
String temp = new String(byteArray, 0, readBytes);
sb.append(temp);
String inData = sb.toString();
System.out.println(inData);
form.append(inData);
c.close();
}
} catch (IOException ex) {
form.append("Connection failed.");
System.out.println("here");
}
// if (s != null) {
// form.append(inData);
// } else if (s == null) {
// form.append("Connection failed.");
// }
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void commandAction(Command c, Displayable d){
if(c == command)
destroyApp(true);
}
}
输出控制台的快照如下:
Installing suite from: file:///C:/Users/Messi/Documents/NetBeansProjects/networking/dist/nbrun3458695790676134373/networking.jad
988830 - ERROR - AMS - 2 - Installation failed: com.sun.midp.midletsuite.MIDletSuiteLockedException
com.sun.midp.midletsuite.MIDletSuiteLockedException
at com.sun.midp.midletsuite.MIDletSuiteImpl.lockMIDletSuite(), bci=0
at com.sun.midp.midletsuite.MIDletSuiteStorage.getMIDletSuite(), bci=12
at com.sun.midp.installer.Installer.checkPreviousVersion(), bci=60
at com.sun.midp.installer.Installer.installStep2(), bci=10
at com.sun.midp.installer.Installer.performInstall(), bci=132
at com.sun.midp.installer.Installer.resumeInstallation(), bci=7
at com.sun.midp.installer.MidpInstaller$StartAction.run(), bci=10
at com.sun.j2me.security.AccessController.doPrivileged(), bci=12
at com.sun.midp.installer.MidpInstaller$InstallThread.run(), bci=9
at java.lang.Thread.run(Thread.java:743)
javacall_lifecycle_state_changed() lifecycle: event is JAVACALL_LIFECYCLE_MIDLET_SHUTDOWN status is JAVACALL_OK
javax.microedition.io.ConnectionNotFoundException: error 10061 in socket::open
at com.sun.midp.io.j2me.socket.Protocol.open0(), bci=0
at com.sun.midp.io.j2me.socket.Protocol.connect(), bci=184
at com.sun.midp.io.j2me.socket.Protocol.open(), bci=216
at com.sun.midp.io.j2me.socket.Protocol.openPrim(), bci=4
at com.sun.midp.io.j2me.http.Protocol.createConnection(), bci=41
at com.sun.midp.io.j2me.http.Protocol.connect(), bci=41
at com.sun.midp.io.j2me.http.Protocol.streamConnect(), bci=164
at com.sun.midp.io.j2me.http.Protocol.startRequest(), bci=7
at com.sun.midp.io.j2me.http.Protocol.sendRequest(), bci=33
at com.sun.midp.io.j2me.http.Protocol.sendRequest(), bci=3
at com.sun.midp.io.j2me.http.Protocol.getResponseCode(), bci=5
- networking.Main.run(Main.java:68)
at java.lang.Thread.run(Thread.java:743)
任何帮助将不胜感激。谢谢!