J2ME connecte til en server (central) via Proxy
Hej jeg har lavet følgende kode. Mit problem er at hver gang jeg prøver at kommer ud over skolens LAN. Når jeg f.eks. prøver at tilgå følgende url:String url =" http://www.cs.auc.dk/~mzinck/index.html";får jeg følgende fejl:
java.lang.ClassNotFoundException: com.sun.midp.io.j2me. http.Protocol
at javax.microedition.io.Connector.openPrim(+99)
at javax.microedition.io.Connector.open(+15)
at javax.microedition.io.Connector.open(+6)
at javax.microedition.io.Connector.open(+5)
at HitMIDlet.connect(+24)
at HitMIDlet.access$000(+4)
at HitMIDlet$1.run(+7)
Exception: java/lang/ClassNotFoundException
java.lang.ClassNotFoundException: com.sun.midp.io.j2me. http.Protocol
at javax.microedition.io.Connector.openPrim(+99)
at javax.microedition.io.Connector.open(+40)
at javax.microedition.io.Connector.open(+6)
at javax.microedition.io.Connector.open(+5)
at HitMIDlet.connect(+24)
at HitMIDlet.access$000(+4)
at HitMIDlet$1.run(+7)
Exception: javax/microedition/io/ConnectionNotFoundExceptio
men når jeg tilgår url'en
String url http://localhost/~knoppix/index.html, er der ingen fejl og det virker. Jeg kan kun connecte til localhost. Jeg har defineret proxy serveren i Wireless Toolkittet ver 2.0, men det er som det ikke rigtigt virker. Kan man istedet måske hardkode proxyen i source kodet ???
// Source koden, der kan connecte til PHP localhost
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HitMIDlet extends MIDlet implements CommandListener
{
private Display mDisplay;
private Form mMainForm;
private StringItem mMessageItem;
private Command mExitCommand, mConnectCommand;
public HitMIDlet()
{
mMainForm = new Form("HitMIDlet");
mMessageItem = new StringItem(null, "");
mExitCommand = new Command("Exit", Command.EXIT, 0);
mConnectCommand = new Command("Connect",Command.SCREEN, 0);
mMainForm.append(mMessageItem);
mMainForm.addCommand(mExitCommand);
mMainForm.addCommand(mConnectCommand);
mMainForm.setCommandListener(this);
}
public void startApp()
{
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mMainForm);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
if (c == mExitCommand)
notifyDestroyed();
else if (c == mConnectCommand)
{
Form waitForm = new Form("Waiting...");
mDisplay.setCurrent(waitForm);
Thread t = new Thread()
{
public void run()
{
connect();
}
};
t.start();
}
}
private void connect()
{
mMessageItem.setText("Connecting");
HttpConnection http = null;
InputStream iStrm = null;
ByteArrayOutputStream bStrm = null;
// When using GET, client data is stored in environment variables on the server; thus,
// there is a limit to the size of a request. With POST, because data is sent as a stream,
// there is no limit to how much data can be processed.
//InputStream in = null;
//String url = getAppProperty("HitMIDlet.URL");
// With localhost, Resin Server and a servlet on the server
//String url = ("http://localhost:8080/resin-doc/servlet/tutorial/J2METest/test");
// with localhost, Apache server and a PHP file on the server
//String url ="http://localhost/~knoppix/J2MEhello.php";
// with 192.168.195.97, Apache server and a PHP file on the server
//String url = "http://192.168.195.97/~thomasw/helloJ2ME.php";
//String url =" http://www.cs.auc.dk/~mzinck/index.html"; // virker IKKE !!kan ikke komme ud over localhost
//String url ="http://localhost/~knoppix/index.html";
try
{
// Create the connection
http = (HttpConnection) Connector.open(url);
//hc = (HttpConnection)Connector.open(url);
mMessageItem.setText("Url opened");
http.setRequestMethod(HttpConnection.GET);
http.setRequestProperty("Connection", "close");
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
int ch;
iStrm = http.openInputStream();
mMessageItem.setText("inputStream opened");
bStrm = new ByteArrayOutputStream();
while ((ch = iStrm.read()) != -1)
{
// Ignore any carriage returns/linefeeds
if (ch != 10 && ch != 13)
bStrm.write(ch);
}
iStrm.close();
http.close();
// Show the response to the user.
mMessageItem.setText(new String(bStrm.toByteArray()));
}
}
catch (IOException ioe)
{
mMessageItem.setText(ioe.toString());
}
mDisplay.setCurrent(mMainForm);
}
}
