📄 httpconnectiondemo.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.* ;
import java.io.*;
public class HttpConnectionDemo extends MIDlet
implements CommandListener
{
String url = "http://localhost/HttpConnectionDemo.asp" ;
Form mainForm = new Form ("HttpConnectionDemo");
TextField nameField =
new TextField ("姓名", "唐先生", 15, TextField.ANY);
TextField ageField =
new TextField ("年龄", "18", 5, TextField.NUMERIC);
StringItem resultItem =
new StringItem ("", "");
Command getCmd =
new Command ("Get", Command.SCREEN, 1);
Command postCmd =
new Command ("Post", Command.SCREEN, 0);
Command exitCmd =
new Command ("Exit", Command.EXIT, 0);
public HttpConnectionDemo () {
mainForm.append (nameField);
mainForm.append (ageField);
mainForm.append (resultItem);
mainForm.addCommand (getCmd);
mainForm.addCommand (postCmd);
mainForm.addCommand (exitCmd);
mainForm.setCommandListener (this);
}
public void startApp()
{
Display.getDisplay (this).setCurrent (mainForm);
try{
getDemo( nameField.getString(), ageField.getString() );
}catch(Exception e)
{
System.out.println( e );
}
}
// 使用GET方式
void getDemo(String name, String age) throws Exception
{
HttpConnection c = null;
try
{
String query = "name=" + name;
query += "&" + "age=" + age;
c = (HttpConnection)Connector.open(
url + "?" + query );
c.setRequestMethod(HttpConnection.GET);
readFromWeb( c );
}
catch (ClassCastException e)
{
throw new IllegalArgumentException("Not an HTTP URL");
}
}
// 使用POST方式
void postDemo(String name, String age) throws Exception
{
String info = "name=" + name;
info += "&" + "age=" + age;
HttpConnection c = null;
OutputStream os = null;
try
{
c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("CONTENT-TYPE",
"application/x-www-form-urlencoded");
os = c.openOutputStream();
byte [] data = info.getBytes("GB2312");
for( int i=0; i<data.length; i++ )
os.write( (byte) data[i] );
readFromWeb( c );
}
catch (ClassCastException e)
{
throw new IllegalArgumentException("Not an HTTP URL");
}
}
// 获取从服务器返回的结果
void readFromWeb( HttpConnection c ) throws Exception
{
InputStream is = null;
try
{
int rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK)
{
//throw new Exception("HTTP response code: " + rc);
}
System.out.println( rc );
is = c.openInputStream();
String type = c.getType();
System.out.println( type );
int len = (int)c.getLength();
System.out.println( len );
if (len > 0)
{
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1))
{
actual = is.read(
data, bytesread, len - bytesread);
bytesread += actual;
}
process(data);
}
else
{
int ch;
while ((ch = is.read()) != -1)
{
process((byte)ch);
}
}
}
finally
{
if (is != null)
is.close();
}
}
void process(byte[] data)
{
String str = "";
try{
str = new String(data, "GB2312");
}
catch(java.io.UnsupportedEncodingException e )
{
System.out.println( e );
}
System.out.println( str );
resultItem.setText( str );
}
void process(byte ch)
{
resultItem.setText( resultItem.getText() + (char) ch );
}
public void pauseApp()
{}
public void destroyApp(boolean unconditional)
{}
public void commandAction(javax.microedition.lcdui.Command c,
javax.microedition.lcdui.Displayable d)
{
try
{
if(c.equals(getCmd))
{
Object cSR;
getDemo(
nameField.getString(), ageField.getString() );
}
else if(c.equals(postCmd))
{
postDemo(
nameField.getString(), ageField.getString() );
}
else if(c.equals(exitCmd))
{
destroyApp(false);
notifyDestroyed();
}
}
catch (Exception e)
{
e.printStackTrace();
resultItem.setLabel("Error: " + e);
System.out.println(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -