Use HttpConnection : HttpConnection « J2ME « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » J2ME » HttpConnection 
31. 37. 2. Use HttpConnection
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.Command;
import javax.microedition.lcdui.CommandListener;
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 = (HttpConnectionConnector.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;
    }
  }
}
31. 37. HttpConnection
31. 37. 1. An example MIDlet to fetch a page using an HttpConnection
31. 37. 2. Use HttpConnection
31. 37. 3. An example MIDlet to invoke a CGI script (POST method is used).
31. 37. 4. Load Image with HttpConnection
31. 37. 5. Response Header
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.