新聞中心
首先,我將討論一下HttpConnection接口,這個(gè)接口可以用來(lái)建立Http連接

HttpConnection 接口
Connected Limited Device Configuration(有限連接設(shè)備配置。簡(jiǎn)稱CLDC)提供了一套用于網(wǎng)絡(luò)連接的類,就是普通連接框架。一種平臺(tái)獨(dú)立連接框架,提供了一種分層的連接接口,它的實(shí)現(xiàn)操作系統(tǒng)由具體的設(shè)備簡(jiǎn)表提供(比如Mobile Information Device Profile(MIDP))。
MIDP通過(guò)提供支持HTTP的HttpConnection 框架來(lái)實(shí)現(xiàn)擴(kuò)展CLDC的一般連接框架的作用。所有MIDP的應(yīng)用程序?qū)崿F(xiàn)都要求支持HTTP,這主要是因?yàn)镠TTP即可以通過(guò)使用基于IP的協(xié)議(如 TCP/IP)也可以通過(guò)使用非IP協(xié)議(如WAP)來(lái)實(shí)現(xiàn)。
所有的連接都是使用Connector類的open()方法來(lái)創(chuàng)建的,如果連接成功的話,這個(gè)方法就返回一個(gè)實(shí)現(xiàn)某種普通連接借口的對(duì)象,舉一個(gè)例子吧,下面的代碼段可以用來(lái)打開(kāi)一個(gè)到某個(gè)URL的HTTP連接。
String url =http://www.ora.com/whatif.jsp;;
HttpConnection connection = Connector.open(url);
一但一個(gè)連接被建立后,就可以設(shè)置屬性了,然后就可以建立I/O流來(lái)發(fā)送或接收數(shù)據(jù)。舉個(gè)例子,請(qǐng)看下面的這一小段代碼,用來(lái)設(shè)置屬性并建立輸入/輸出流。 // 設(shè)置 HTTP 屬性
- // 設(shè)置 HTTP 屬性
- connection.setRequestMethod(HttpConnection.POST);
- connection.setRequestProperty("IF-Modified-Since","22 Dec 2001 16:33:19 GMT");
- connection.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
- connection.setRequestProperty("Content-Language", "en-CA");
- // 創(chuàng)建I/O流
- InputStream is = connection.openInputStream();
- OutputStream os = connection.openOutputStream();
這個(gè)JSP里面希望取得一個(gè)名為name 的變量的值,一旦這個(gè)值被取得,就會(huì)創(chuàng)建一個(gè)Date的實(shí)例,然后name和date的值就會(huì)被打到客戶端中的輸出流中。
現(xiàn)在,讓我們看看如何寫(xiě)一個(gè)MIDlet來(lái)調(diào)用這個(gè)JSP頁(yè)面,我們將使用POST請(qǐng)求方法來(lái)調(diào)用它,這就意味著被傳送到JSP頁(yè)面的數(shù)據(jù)不是使用URL編碼的,而是以一段單獨(dú)的信息傳入,這段MIDlet代碼如代碼段2所示。
代碼2:
- InvokeJSPMidlet.java
- import javax.microedition.lcdui.*;
- import javax.microedition.midlet.*;
- import javax.microedition.io.*;
- import java.io.*;
- public class InvokeJSPMidlet extends MIDlet implements CommandListener {;
- Display display = null;
- // name 字段
- TextField name = null;
- form form;
- String url = "http://127.0.0.1:8080/examples/jsp/today.jsp";;
- static final Command callCommand = new Command("date?", Command.OK, 2);
- static final Command clearCommand = new Command("clear", Command.STOP, 2);
- String myname;
- public InvokeJSPMidlet() {;
- display = Display.getDisplay(this);
- name = new TextField("Name:", " ", 25, TextField.ANY);
- form = new form("Invoke JSP");
- };
- public void startApp() throws MIDletStateChangeException {;
- form.append(name);
- form.addCommand(clearCommand);
- form.addCommand(callCommand);
- form.setCommandListener(this);
- display.setCurrent(form);
- };
- public void pauseApp() {;
- };
- public void destroyApp(boolean unconditional) {;
- notifyDestroyed();
- };
- void invokeJSP(String url) throws IOException {;
- HttpConnection c = null;
- InputStream is = null;
- OutputStream os = null;
- StringBuffer b = new StringBuffer();
- TextBox t = null;
- try {;
- c = (HttpConnection)Connector.open(url);
- c.setRequestMethod(HttpConnection.POST);
- c.setRequestProperty("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT");
- c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
- c.setRequestProperty("Content-Language", "en-CA");
- c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- os = c.openOutputStream();
- os.write(("name="+myname).getBytes());
- os.flush();
- is = c.openDataInputStream();
- int ch;
- while ((ch = is.read()) != -1) {;
- b.append((char) ch);
- System.out.print((char)ch);
- };
- t = new TextBox("Date", b.toString(), 1024, 0);
- t.setCommandListener(this);
- }; finally {;
- if(is!= null) {;
- is.close();
- };
- if(os != null) {;
- os.close();
- };
- if(c != null) {;
- c.close();
- };
- };
- display.setCurrent(t);
- };
- public void commandAction(Command c, Displayable d) {;
- String label = c.getLabel();
- if(label.equals("clear")) {;
- destroyApp(true);
- };
- else if (label.equals("date?")) {;
- myname = name.getString();
- try {;
- invokeJSP(url);
- };catch(IOException e) {;
- };
- };
- };
- };
InvokeJSPMidlet代碼指定了要被調(diào)用的JSP頁(yè)面的URL,然后就創(chuàng)建了兩個(gè)命令按鈕,然后創(chuàng)建一個(gè)text字段,可以讓用戶在里面輸入姓名。在InvokeJSP()方法中,將建立一個(gè)到這個(gè)URL的HTTP連接,然后再建立I/O流,MIDlet使用輸出流來(lái)發(fā)送數(shù)據(jù)到JSP頁(yè)面,接著再使用輸入流從JSP頁(yè)面中接收數(shù)據(jù),注意,在本例中我們將發(fā)送姓名到JSP頁(yè)面中,其實(shí)它也只是向你演示一下數(shù)據(jù)如何在MIDlet和頁(yè)面之間流通。
在代碼段2中,應(yīng)當(dāng)注意的事情是為了使JSP頁(yè)面使用getParameter()從name變量中取得數(shù)據(jù)的值,你必須設(shè)置Content-Type屬性為application/x-www-form-urlencoded.
小結(jié)
本文只是演示如何從MIDlet中調(diào)用JSP頁(yè)面,InvokeJSPMidlet還可以很容易的修改來(lái)實(shí)現(xiàn)調(diào)用其他的JSP的目的。但是注意,JSP主要和HTML配合使用,但是如果你的移動(dòng)設(shè)備中的瀏覽器不能處理HTML的話,那么XML也是一個(gè)非常好的選擇,因?yàn)镸IDlet可以解析XML文檔。
本文題目:如何從MIDlet中調(diào)用JSP頁(yè)面
網(wǎng)頁(yè)鏈接:http://www.dlmjj.cn/article/dpojicp.html


咨詢
建站咨詢
