Java by API/javax.microedition.io/HttpConnection
Содержание
- 1 HttpConnection: getFile()
- 2 HttpConnection: getHeaderField(String value) throws IOException
- 3 HttpConnection: getPort()
- 4 HttpConnection: getProtocol()
- 5 HttpConnection: getQuery()
- 6 HttpConnection: getRef()
- 7 HttpConnection: getRequestMethod()
- 8 HttpConnection: getURL()
- 9 HttpConnection.HTTP_OK
- 10 HttpConnection: setRequestProperty(String key, String value) throws IOException
- 11 InputConnection: openInputStream() throws IOException
HttpConnection: getFile()
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class ConnectHttpMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand= new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://www.g.ru/p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public ConnectHttpMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Data");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "failed";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost() + "\n";
content += "Port: " + hpc.getPort() + "\n";
content += "File: " + hpc.getFile() + "\n";
content += "Query: " + hpc.getQuery() + "\n";
content += "reference: " + hpc.getRef() + "\n";
content += "request method: " + hpc.getRequestMethod();
if (hpc != null)
hpc.close();
} catch (IOException e2) {
}
return content;
}
}
HttpConnection: getHeaderField(String value) throws IOException
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class CookieMIDlet extends MIDlet implements CommandListener, Runnable {
private Display mDisplay;
private Form mForm;
private String mSession;
public void startApp() {
mDisplay = Display.getDisplay(this);
mForm = new Form("CookieMIDlet");
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
mForm.addCommand(new Command("Send", Command.SCREEN, 0));
mForm.setCommandListener(this);
mDisplay.setCurrent(mForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
else {
Form waitForm = new Form("Connecting...");
mDisplay.setCurrent(waitForm);
Thread t = new Thread(this);
t.start();
}
}
public void run() {
String url = getAppProperty("CookieMIDlet-URL");
try {
HttpConnection hc = (HttpConnection) Connector.open(url);
if (mSession != null)
hc.setRequestProperty("cookie", mSession);
InputStream in = hc.openInputStream();
String cookie = hc.getHeaderField("Set-cookie");
if (cookie != null) {
int semicolon = cookie.indexOf(";");
mSession = cookie.substring(0, semicolon);
}
int length = (int) hc.getLength();
byte[] raw = new byte[length];
in.read(raw);
String s = new String(raw);
Alert a = new Alert("Response", s, null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mForm);
in.close();
hc.close();
} catch (IOException ioe) {
Alert a = new Alert("Exception", ioe.toString(), null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mForm);
}
}
}
HttpConnection: getPort()
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class ConnectHttpMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand= new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://www.g.ru/p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public ConnectHttpMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Data");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "failed";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost() + "\n";
content += "Port: " + hpc.getPort() + "\n";
content += "File: " + hpc.getFile() + "\n";
content += "Query: " + hpc.getQuery() + "\n";
content += "reference: " + hpc.getRef() + "\n";
content += "request method: " + hpc.getRequestMethod();
if (hpc != null)
hpc.close();
} catch (IOException e2) {
}
return content;
}
}
HttpConnection: getProtocol()
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class ConnectHttpMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand= new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://www.g.ru/p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public ConnectHttpMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Data");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "failed";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost() + "\n";
content += "Port: " + hpc.getPort() + "\n";
content += "File: " + hpc.getFile() + "\n";
content += "Query: " + hpc.getQuery() + "\n";
content += "reference: " + hpc.getRef() + "\n";
content += "request method: " + hpc.getRequestMethod();
if (hpc != null)
hpc.close();
} catch (IOException e2) {
}
return content;
}
}
HttpConnection: getQuery()
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class ConnectHttpMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand= new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://www.g.ru/p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public ConnectHttpMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Data");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "failed";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost() + "\n";
content += "Port: " + hpc.getPort() + "\n";
content += "File: " + hpc.getFile() + "\n";
content += "Query: " + hpc.getQuery() + "\n";
content += "reference: " + hpc.getRef() + "\n";
content += "request method: " + hpc.getRequestMethod();
if (hpc != null)
hpc.close();
} catch (IOException e2) {
}
return content;
}
}
HttpConnection: getRef()
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class ConnectHttpMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand= new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://www.g.ru/p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public ConnectHttpMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Data");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "failed";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost() + "\n";
content += "Port: " + hpc.getPort() + "\n";
content += "File: " + hpc.getFile() + "\n";
content += "Query: " + hpc.getQuery() + "\n";
content += "reference: " + hpc.getRef() + "\n";
content += "request method: " + hpc.getRequestMethod();
if (hpc != null)
hpc.close();
} catch (IOException e2) {
}
return content;
}
}
HttpConnection: getRequestMethod()
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class ConnectHttpMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand= new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://www.g.ru/p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public ConnectHttpMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Data");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "failed";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost() + "\n";
content += "Port: " + hpc.getPort() + "\n";
content += "File: " + hpc.getFile() + "\n";
content += "Query: " + hpc.getQuery() + "\n";
content += "reference: " + hpc.getRef() + "\n";
content += "request method: " + hpc.getRequestMethod();
if (hpc != null)
hpc.close();
} catch (IOException e2) {
}
return content;
}
}
HttpConnection: getURL()
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class ConnectHttpMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand= new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://www.g.ru/p=MDIP&u=b&y=y&hc=0&hs=0";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public ConnectHttpMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Data");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "failed";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
content += "URL: " + hpc.getURL() + "\n";
content += "Protocol: " + hpc.getProtocol() + "\n";
content += "Host: " + hpc.getHost() + "\n";
content += "Port: " + hpc.getPort() + "\n";
content += "File: " + hpc.getFile() + "\n";
content += "Query: " + hpc.getQuery() + "\n";
content += "reference: " + hpc.getRef() + "\n";
content += "request method: " + hpc.getRequestMethod();
if (hpc != null)
hpc.close();
} catch (IOException e2) {
}
return content;
}
}
HttpConnection.HTTP_OK
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HttpMIDlet extends MIDlet implements CommandListener, Runnable {
private Display display;
private Form addressForm = new Form("HTTP Client");
private Form connectForm = new Form("Connecting");
private Form displayForm = new Form("Server Reply");
private TextField serverURL = new TextField("URL:", "", 256, TextField.ANY);
private StringItem messageLabel = new StringItem(null, "Connecting...\nPlease wait.");
private Command okCommand = new Command("OK", Command.OK, 0);
private Command exitCommand = new Command("Exit", Command.EXIT, 0);
private Command backCommand = new Command("Back", Command.BACK, 0);
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
addressForm.append(serverURL);
addressForm.addCommand(okCommand);
addressForm.addCommand(exitCommand);
addressForm.setCommandListener(this);
connectForm.append(messageLabel);
connectForm.addCommand(backCommand);
connectForm.setCommandListener(this);
displayForm.append(messageLabel);
displayForm.addCommand(backCommand);
displayForm.setCommandListener(this);
display.setCurrent(addressForm);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
public void commandAction(Command cmd, Displayable d) {
if (cmd == okCommand) {
Thread t = new Thread(this);
t.start();
display.setCurrent(connectForm);
} else if (cmd == backCommand) {
display.setCurrent(addressForm);
} else if (cmd == exitCommand) {
try {
destroyApp(true);
} catch (MIDletStateChangeException ex) {
}
notifyDestroyed();
}
}
public void run() {
InputStream is = null;
HttpConnection conn = null;
try {
String url = serverURL.getString();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
is = conn.openInputStream();
final int MAX_LENGTH = 128;
byte[] buf = new byte[MAX_LENGTH];
int total = 0;
while (total < MAX_LENGTH) {
int count = is.read(buf, total, MAX_LENGTH - total);
if (count < 0) {
break;
}
total += count;
}
is.close();
String reply = new String(buf, 0, total);
messageLabel.setText(reply);
} else {
messageLabel.setText("Failed: error " + conn.getResponseCode() + "\n"+ conn.getResponseMessage());
}
conn.close();
display.setCurrent(displayForm);
} catch (IOException ex) {
System.out.println(ex);
ex.printStackTrace();
Alert alert = new Alert("I/O Error","An error occurred while communicating with the server.", null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, addressForm);
return;
}
}
}
HttpConnection: setRequestProperty(String key, String value) throws IOException
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class CookieMIDlet extends MIDlet implements CommandListener, Runnable {
private Display mDisplay;
private Form mForm;
private String mSession;
public void startApp() {
mDisplay = Display.getDisplay(this);
mForm = new Form("CookieMIDlet");
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
mForm.addCommand(new Command("Send", Command.SCREEN, 0));
mForm.setCommandListener(this);
mDisplay.setCurrent(mForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
else {
Form waitForm = new Form("Connecting...");
mDisplay.setCurrent(waitForm);
Thread t = new Thread(this);
t.start();
}
}
public void run() {
String url = getAppProperty("CookieMIDlet-URL");
try {
HttpConnection hc = (HttpConnection) Connector.open(url);
if (mSession != null)
hc.setRequestProperty("cookie", mSession);
InputStream in = hc.openInputStream();
String cookie = hc.getHeaderField("Set-cookie");
if (cookie != null) {
int semicolon = cookie.indexOf(";");
mSession = cookie.substring(0, semicolon);
}
int length = (int) hc.getLength();
byte[] raw = new byte[length];
in.read(raw);
String s = new String(raw);
Alert a = new Alert("Response", s, null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mForm);
in.close();
hc.close();
} catch (IOException ioe) {
Alert a = new Alert("Exception", ioe.toString(), null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mForm);
}
}
}
InputConnection: openInputStream() throws IOException
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class HttpGETMIDlet extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command sendCommand = new Command("Send", Command.OK, 1);
private Command backCommand = new Command("Upload", Command.OK, 1);
private Display display;
private String defaultURL = "http://localhost/JIDCA/hello.asp?name=JIDCA";
private Form mainForm, resultForm;
private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);
private StringItem resultItem;
public HttpGETMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
mainForm = new Form("Address");
mainForm.append(URL);
mainForm.addCommand(sendCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == sendCommand) {
String result = "";
resultItem = new StringItem(null, result);
resultForm = new Form("Result");
String URLString = URL.getString();
try {
result = requestUsingGET(URLString);
} catch (IOException e) {
result = "Falied";
}
resultItem.setText(result);
resultForm.append(resultItem);
resultForm.addCommand(backCommand);
resultForm.setCommandListener(this);
display.setCurrent(resultForm);
} else if (c == backCommand) {
URL.setString(defaultURL);
display.setCurrent(mainForm);
} else {
destroyApp(false);
notifyDestroyed();
}
}
private String requestUsingGET(String URLString) throws IOException {
HttpConnection hpc = null;
DataInputStream dis = null;
boolean newline = false;
String content = "";
try {
hpc = (HttpConnection) Connector.open(URLString);
dis = new DataInputStream(hpc.openInputStream());
int character;
while ((character = dis.read()) != -1) {
if ((char) character == "\\") {
newline = true;
continue;
} else {
if ((char) character == "n" && newline) {
content += "\n";
newline = false;
} else if (newline) {
content += "\\" + (char) character;
newline = false;
} else {
content += (char) character;
newline = false;
}
}
}
if (hpc != null)
hpc.close();
if (dis != null)
dis.close();
} catch (IOException e2) {
}
return content;
}
}