Java Tutorial/J2ME/Connector

Материал из Java эксперт
Перейти к: навигация, поиск

Call asp page from J2ME

   <source lang="java">

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 {

 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(new CommandListener() {
     public void commandAction(Command c, Displayable s) {
       if (c.getCommandType() == Command.EXIT)
         notifyDestroyed();
       else
         send();
     }
   });
   mDisplay.setCurrent(mForm);
 }
 private void send() {
   String url = "http://localhost/http.asp";
   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);
   }
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }

}</source>





Check Http connection returning code

   <source lang="java">

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 HttpOKMIDlet 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/";
 private Form mainForm, resultForm;
 private TextField URL= new TextField(null, defaultURL, 250, TextField.URL);
 private StringItem resultItem;
 public HttpOKMIDlet() {
   display = Display.getDisplay(this);
 }
 public void startApp() {
   mainForm = new Form("Test");
   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;
   String content = "";
   try {
     hpc = (HttpConnection) Connector.open(URLString);
     int status = hpc.getResponseCode();
     if (status != HttpConnection.HTTP_OK)
       content = "failed";
     else
       content = "connected";
     if (hpc != null)
       hpc.close();
   } catch (IOException e2) {
   }
   return content;
 }

}</source>





Http Get with J2ME

   <source lang="java">

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;
 }

}</source>





Http header

   <source lang="java">

import java.io.DataInputStream; import java.io.DataOutputStream; 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 HttpHEADMIDlet 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/";
 private Form mainForm, resultForm;
 private TextField URL= new TextField(null, defaultURL, 250, TextField.URL);
 private StringItem resultItem;
 public HttpHEADMIDlet() {
   display = Display.getDisplay(this);
 }
 public void startApp() {
   mainForm = new Form("HEDA Method");
   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;
   String content = "";
   try {
     hpc = (HttpConnection) Connector.open(URLString);
     hpc.setRequestMethod(HttpConnection.HEAD);
     dis = new DataInputStream(hpc.openInputStream());
     int i = 1;
     String key = "";
     String value = "";
     while ((value = hpc.getHeaderField(i)) != null) {
       key = hpc.getHeaderFieldKey(i++);
       content += key + ":" + value + "\n";
     }
     if (hpc != null)
       hpc.close();
     if (dis != null)
       dis.close();
   } catch (IOException e2) {
   }
   return content;
 }

}</source>





Http post

   <source lang="java">

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; 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 HttpPOSTMIDlet 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/fetchNews.asp";
 private Form mainForm, resultForm;
 private TextField URL = new TextField(null, null, 250, TextField.URL);
 private StringItem resultItem;
 public HttpPOSTMIDlet() {
   display = Display.getDisplay(this);
 }
 public void startApp() {
   mainForm = new Form("Name");
   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 = "name=" + 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;
   DataOutputStream dos = null;
   boolean newline = false;
   String content = "";
   try {
     hpc = (HttpConnection) Connector.open(defaultURL);
     hpc.setRequestMethod(HttpConnection.POST);
     hpc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
     hpc.setRequestProperty("Content-Language", "zh-tw");
     hpc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
     hpc.setRequestProperty("Content-Length", String.valueOf(URLString.length()));
     dos = new DataOutputStream(hpc.openOutputStream());
     dos.write(URLString.getBytes());
     dos.flush();
     InputStreamReader xdis = new InputStreamReader(hpc.openInputStream());
     int character;
     while ((character = xdis.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;
 }

}</source>





Image Loader

   <source lang="java">

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.Image; import javax.microedition.lcdui.ImageItem; import javax.microedition.midlet.MIDlet; public class J2MEImageLoader extends MIDlet implements CommandListener, Runnable {

 private Display mDisplay;
 private Form mForm = new Form("Connecting...");
 public J2MEImageLoader() {
   mForm.addCommand(new Command("Exit", Command.EXIT, 0));
   mForm.setCommandListener(this);
 }
 public void startApp() {
   mDisplay = Display.getDisplay(this);
   mDisplay.setCurrent(mForm);
   Thread t = new Thread(this);
   t.start();
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }
 public void commandAction(Command c, Displayable s) {
   if (c.getCommandType() == Command.EXIT)
     notifyDestroyed();
 }
 public void run() {
   HttpConnection hc = null;
   DataInputStream in = null;
   try {
     hc = (HttpConnection) Connector.open("http://www.y.ru/image.gif");
     int length = (int) hc.getLength();
     byte[] data = null;
     if (length != -1) {
       data = new byte[length];
       in = new DataInputStream(hc.openInputStream());
       in.readFully(data);
     } else {
       int chunkSize = 512;
       int index = 0;
       int readLength = 0;
       in = new DataInputStream(hc.openInputStream());
       data = new byte[chunkSize];
       do {
         if (data.length < index + chunkSize) {
           byte[] newData = new byte[index + chunkSize];
           System.arraycopy(data, 0, newData, 0, data.length);
           data = newData;
         }
         readLength = in.read(data, index, chunkSize);
         index += readLength;
       } while (readLength == chunkSize);
       length = index;
     }
     Image image = Image.createImage(data, 0, length);
     ImageItem imageItem = new ImageItem(null, image, 0, null);
     mForm.append(imageItem);
     mForm.setTitle("Done.");
   } catch (IOException ioe) {
     mForm.setTitle("Error.");
   }
 }

}</source>





Load image from a URL

   <source lang="java">

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.Image; import javax.microedition.lcdui.ImageItem; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.StringItem; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; public class HttpMIDlet 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 URL = "http://localhost/Java.png";
 private Form mainForm, resultForm;
 private Item mItem;
 private TextField URLField = new TextField(null, URL, 250, TextField.URL);
 public HttpMIDlet() {
   display = Display.getDisplay(this);
 }
 public void startApp() {
   mainForm = new Form("Address");
   mainForm.append(URLField);
   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) {
     resultForm = new Form("Image");
     String URLString = URLField.getString();
     try {
       Image image = loadImage(URLString);
       mItem = new ImageItem(null, image, 0, null);
     } catch (IOException ioe) {
       mItem = new StringItem(null, ioe.toString());
     }
     resultForm.append(mItem);
     resultForm.addCommand(backCommand);
     resultForm.setCommandListener(this);
     display.setCurrent(resultForm);
   } else if (c == backCommand) {
     URLField.setString(URL);
     display.setCurrent(mainForm);
   } else {
     destroyApp(false);
     notifyDestroyed();
   }
 }
 public Image loadImage(String url) throws IOException {
   HttpConnection hpc = null;
   DataInputStream dis = null;
   try {
     hpc = (HttpConnection) Connector.open(url);
     int length = (int) hpc.getLength();
     byte[] data = new byte[length];
     dis = new DataInputStream(hpc.openInputStream());
     dis.readFully(data);
     return Image.createImage(data, 0, data.length);
   } finally {
     if (hpc != null)
       hpc.close();
     if (dis != null)
       dis.close();
   }
 }

}</source>





URL query

   <source lang="java">

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;
 }

}</source>