`
isiqi
  • 浏览: 16034470 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

J2ME HTTP方式与服务器交互信息:GET方式和POST方式

阅读更多

服务器端程序:

读取手机发送过来的两个参数信息,一个是用户账号account,一个是用户密码password,然后servlet

正确读取两个信息以后把两个参数返回给手机程序。

 1:  package com.example.servlets;
 2:  
 3:  import java.io.IOException;
 4:  import java.io.PrintWriter;
 5:  
 6:  import javax.servlet.ServletException;
 7:  import javax.servlet.http.HttpServlet;
 8:  import javax.servlet.http.HttpServletRequest;
 9:  import javax.servlet.http.HttpServletResponse;
10:  
11:  /**
12:   * Servlet implementation class HelloServlet
13:   */
14:  public class HelloServlet extends HttpServlet {
15:      private static final long serialVersionUID = 1L;
16:  
17:      /**
18:       * @see HttpServlet#HttpServlet()
19:       */
20:      public HelloServlet() {
21:          super();
22:          // TODO Auto-generated constructor stub
23:      }
24:  
25:      /**
26:       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
27:       *      response)
28:       */
29:      protected void doGet(HttpServletRequest request,
30:              HttpServletResponse response) throws ServletException, IOException {
31:          // response.getWriter().write("Hello, world!");
32:          // 从MIDlet发送的信息中读取参数
33:          String acct = request.getParameter("account"), pwd = request
34:                  .getParameter("password");
35:          if (acct == null || pwd == null) {
36:              response.sendError(HttpServletResponse.SC_BAD_REQUEST,
37:                      "Unable to read parameters");
38:              return;
39:          }
40:  
41:          // 返回信息给客户端
42:          response.setContentType("text/plain");
43:          // 以网页输出格式返回信息
44:          PrintWriter out = response.getWriter();
45:          out.print(this.getServletInfo() + "\n");
46:          out.print("acct:" + acct + "\npwd:" + pwd);
47:          out.close();
48:      }
49:  
50:      /**
51:       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
52:       *      response)
53:       */
54:      protected void doPost(HttpServletRequest request,
55:              HttpServletResponse response) throws ServletException, IOException {
56:          doGet(request, response);
57:      }
58:  
59:      // 返回服务器信息
60:      public String getServletInfo() {
61:          return "Hello,World\n\r";
62:      }
63:  
64:  }
65:  

GET方式与服务器交互(客户端):

  1:  import java.io.IOException;
  2:  import java.io.InputStream;
  3:  import javax.microedition.io.Connector;
  4:  import javax.microedition.io.HttpConnection;
  5:  import javax.microedition.lcdui.Command;
  6:  import javax.microedition.lcdui.CommandListener;
  7:  import javax.microedition.lcdui.Display;
  8:  import javax.microedition.lcdui.Displayable;
  9:  import javax.microedition.lcdui.Form;
 10:  import javax.microedition.midlet.MIDlet;
 11:  import javax.microedition.midlet.MIDletStateChangeException;
 12:  
 13:  public class HttpGetExample extends MIDlet implements CommandListener {
 14:  
 15:      private Display display;
 16:      private Command exitCommand;
 17:      private Form mainForm;
 18:      private String password = "newuser";// 用户名
 19:      private String account = "123456";// 用户密码
 20:  
 21:      public HttpGetExample() {
 22:          display = Display.getDisplay(this);
 23:          exitCommand = new Command("Exit", Command.EXIT, 1);
 24:          mainForm = new Form("Data from servlet");
 25:          mainForm.addCommand(exitCommand);
 26:          mainForm.setCommandListener(this);
 27:      }
 28:  
 29:      protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 30:          // TODO Auto-generated method stub
 31:  
 32:      }
 33:  
 34:      protected void pauseApp() {
 35:          // TODO Auto-generated method stub
 36:  
 37:      }
 38:  
 39:      protected void startApp() throws MIDletStateChangeException {
 40:          display.setCurrent(mainForm);
 41:          // 访问服务器servlet
 42:          try {
 43:              callServlet();
 44:          } catch (IOException e) {
 45:              e.printStackTrace();
 46:          }
 47:      }
 48:  
 49:      private void callServlet() throws IOException {
 50:          HttpConnection http = null;
 51:          InputStream iStrm = null;
 52:          // 使用GET方式连接服务器
 53:          String url = "http://127.0.0.1:8080/ExampleWebProject/HelloServlet"
 54:                  + "?" + "account=" + account + "&" + "password=" + password;
 55:          try {
 56:              http = (HttpConnection) Connector.open(url);
 57:              // 使用HttpConnection.GET方式
 58:              // 发送Gety请求
 59:              http.setRequestMethod(HttpConnection.GET);
 60:  
 61:              // 服务器响应
 62:              if (http.getResponseCode() == HttpConnection.HTTP_OK) {
 63:                  iStrm = http.openInputStream();
 64:                  // 获得数据字节数
 65:                  int length = (int) http.getLength();
 66:                  // 获得数据信息
 67:                  if (length > 0) {
 68:                      byte servletData[] = new byte[length];
 69:                      iStrm.read(servletData);
 70:                      // 显示返回信息
 71:                      mainForm.append("验证通过:\n" + new String(servletData));
 72:                  } else {
 73:                      mainForm.append("不能访问数据!");
 74:                  }
 75:              }
 76:          } catch (Exception e) {
 77:              mainForm.append("网络错误");
 78:              e.printStackTrace();
 79:          } finally {
 80:              // 关闭连接对像
 81:              if (iStrm != null) {
 82:                  iStrm.close();
 83:              }
 84:              if (http != null) {
 85:                  http.close();
 86:              }
 87:          }
 88:  
 89:      }
 90:  
 91:      public void commandAction(Command c, Displayable d) {
 92:          if (c == exitCommand) {
 93:              try {
 94:                  destroyApp(false);
 95:                  notifyDestroyed();
 96:              } catch (MIDletStateChangeException e) {
 97:                  // TODO Auto-generated catch block
 98:                  e.printStackTrace();
 99:              }
100:          }
101:      }
102:  
103:  }
104:  

POST方式与服务器交互(客户端):

  1:  import java.io.IOException;
  2:  import java.io.InputStream;
  3:  import java.io.OutputStream;
  4:  
  5:  import javax.microedition.io.Connector;
  6:  import javax.microedition.io.HttpConnection;
line-height: 18px; color: #4f81b

  


  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics