新闻中心

EEPW首页>嵌入式系统>设计应用> 开发MIDP联网应用程序

开发MIDP联网应用程序

作者: 时间:2012-05-07 来源:网络 收藏

importjavax.microedition.io.Connector;

importjavax.microedition.io.HttpConnection;

importjavax.microedition.midlet.MIDlet;

importjavax.microedition.midlet.MIDletStateChangeException;

/**

*利用GET发送request的sample

*从控制台输出response

*/

publicclassGETTestextendsMIDlet{

/**

*访问服务器

*/

protectedvoidstartApp()throwsMIDletStateChangeException{

try{

HttpConnectioncon=(HttpConnection)Connector.open(http://www.nec-mfriend.com/en/);

//指定GET

con.setRequestMethod(HttpConnection.GET);

DataInputStreamin=con.openDataInputStream();

intinput;

while((input=in.read())!=-1){

System.out.print((char)input);

}

in.close();

//关闭链接

con.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

protectedvoidpauseApp(){

}

protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException{

}

}

ex.4

实际操作后的结果。

1.6.利用HEAD

接下来介绍如何利用HEAD方法获取文件的header。多数情况下,在HTTPheader中,包含了文件种类、尺寸大小、文字编码、回复日期、request文件的最后修改时间、以及兑现期限的截止日期等。一般来讲,使用HEAD方法检查其是否对兑现内容进行了新信息的替换。

为使用HEAD,如下所示要在作成的HttpConnection的setRequestMethod方法中,指定HttpConnection的static变量HEAD。

HttpConnectioncon=(HttpConnection)Connector.open(http://www.nec-mfriend.com/en/);

con.setRequestMethod(HttpConnection.HEAD);

获取HEAD信息的方法。

360截图20120507114150638.jpg

表3

下面是利用getHeaderField方法和getHeaderFieldKey方法,获取全部header信息的sample。这个sample与刚才所介绍的一样,是以在模拟器上进行操作为前提而作成的,它只用于说明,实际操作还没有进行测定。由此获取的全部header信息内容将输入控制台。

importjava.io.IOException;

importjavax.microedition.io.Connector;

importjavax.microedition.io.HttpConnection;

importjavax.microedition.midlet.MIDlet;

importjavax.microedition.midlet.MIDletStateChangeException;

/**

*利用HEAD发送request的sample

*从控制台输出response

*/

publicclassHEADTestextendsMIDlet{

/**

*显示header信息

*/

protectedvoidstartApp()throwsMIDletStateChangeException{

try{

HttpConnectioncon=

(HttpConnection)Connector.open(http://www.nec-mfriend.com/en/);

//指定HEAD

con.setRequestMethod(HttpConnection.HEAD);

//取得关键的HTTPheader信息——成对的值

inti=0;

Stringmessage=;

Stringkey=;

Stringvalue=;

while((value=con.getHeaderField(i))!=null){

key=con.getHeaderFieldKey(i++);

message=message+key+:+value+n;

}

System.out.println(message);

//关闭链接

con.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

protectedvoidpauseApp(){

}

protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException{

}

}

ex.5

实际操作后的结果如下所示。

图3

1.7.利用POST

为能利用POST发送request,要使用InputStream和OutputStream。用OutputStream向服务器发送数据,而InputStream则接收来自服务器的response。

用下述方法指定POST。

HttpConnectioncon=(HttpConnection)Connector.open(http://www.yahoo.com);

con.setRequestMethod(HttpConnection.POST);

ex.6

如下所示使用OutputStream在requeat信息中输入数据,使输入数据为(message=helloworld),而变量con是指定了POST的HttpConnection。

Stringmessage=hmessage=helloworldh;

DataOutputStreamdos=con.openDataOutputStream();

byte[]request=message.getBytes();

for(inti=0;i

dos.writeByte(request[i]);

}

dos.flush();

ex.7

下面实际是利用POST与服务器进行通信的sample。在这里,WEB服务器将转发利用POST发送的信息值,并接收最终结果response。接收的response内容将被输入控制台,请用模拟器进行确认。

importjava.io.DataInputStream;

importjava.io.DataOutputStream;

importjava.io.IOException;

importjavax.microedition.io.Connector;

importjavax.microedition.io.HttpConnection;

importjavax.microedition.midlet.MIDlet;

importjavax.microedition.midlet.MIDletStateChangeException;

/**

*利用POST发送request的sample



评论


相关推荐

技术专区

关闭