HTTP头 : 连接器 « 基于J2ME « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 基于J2ME » 连接器 
31. 40. 5. HTTP头
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.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;

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 URLStringthrows IOException {
    HttpConnection hpc = null;
    DataInputStream dis = null;

    String content = "";
    try {
      hpc = (HttpConnectionConnector.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;
  }
}
31. 40. 连接器
31. 40. 1. 图像装载机
31. 40. 2. 网址查询
31. 40. 3. 从J2ME的ASP页面调用
31. 40. 4. 基于J2ME的HTTP GET
31. 40. 5. HTTP头
31. 40. 6. 从URL加载图像
31. 40. 7. 检查HTTP连接返回代码
31. 40. 8. 发送到网址
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.