Response Header : 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. 5. Response Header
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
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 ResponseHeaderMIDlet 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;

  private DateField DateField1 = new DateField("Date", DateField.DATE_TIME);

  private DateField DateField2 = new DateField("Expiration", DateField.DATE_TIME);

  private DateField DateField3 = new DateField("Last Modified", DateField.DATE_TIME);

  private StringItem resultItem;

  public ResponseHeaderMIDlet() {
    display = Display.getDisplay(this);
  }

  public void startApp() {
    URL = new TextField(null, defaultURL, 250, TextField.URL);
    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("");
      String URLString = URL.getString();

      try {
        result = requestUsingGET(URLString);
      catch (IOException e) {
        result = "Falied";
      }

      resultItem.setText(result);

      resultForm.append(resultItem);
      resultForm.append(DateField1);
      resultForm.append(DateField2);
      resultForm.append(DateField3);
      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 URLStringthrows IOException {
    HttpConnection hpc = null;

    String content = "";
    try {
      hpc = (HttpConnectionConnector.open(URLString);

      int status = hpc.getResponseCode();
      if (status != HttpConnection.HTTP_OK)
        content = "Falied";
      else {
        DateField1.setDate(new java.util.Date(hpc.getDate()));
        DateField2.setDate(new java.util.Date(hpc.getExpiration()));
        DateField3.setDate(new java.util.Date(hpc.getLastModified()));

        content += "Content Type: " + hpc.getType() "\n";
        content += "Content Length: " + hpc.getLength() "\n";
        content += "Contnet Encoding: " + hpc.getEncoding() "\n";
      }
      if (hpc != null)
        hpc.close();

    catch (IOException e2) {
    }

    return content;
  }
}
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.