|
リスト2 InetAccess.java
import com.nttdocomo.io.*;
import com.nttdocomo.ui.*;
import com.nttdocomo.util.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;
import java.lang.*;
class NetPanel extends Panel implements ComponentListener {
TextBox msg;
Button btn;
TextBox data;
NetPanel() {
setTitle("Network Connect Test");
add(new Label("Message:") );
msg = new TextBox("Send data.", 200 , 1, TextBox.DISPLAY_ANY );
msg.setInputMode(TextBox.ALPHA);
add(msg);
btn = new Button("接続");
add(btn);
data = new TextBox("" , 50,5,TextBox.DISPLAY_ANY );
add(data);
setComponentListener(this);
}
public void componentAction(Component src, int type, int param) {
if( type == ComponentListener.BUTTON_PRESSED ) {
try {
HttpConnection htcon =
(HttpConnection)Connector.open(
"http://gadget.mda.or.jp/cgi-bin/iapp.cgi",
Connector.READ_WRITE, true );
htcon.setRequestMethod(HttpConnection.POST);
htcon.setRequestProperty("Content-Type","text/plain");
OutputStream htout = htcon.openOutputStream();
String senddata = msg.getText();
htout.write( senddata.getBytes() );
htout.close();
htcon.connect();
InputStream htin = htcon.openInputStream();
if( htin != null ) {
int[] buff = new int[256];
int size = 0;
int d = htin.read();
while ( d != -1 ) {
buff[size++] = d;
if(size > 255)
break;
d = htin.read();
}
byte[] str = new byte[size];
for(int i=0 ; i < size; i++ ) {
str[i] = (byte) (buff[i] & 0xFF);
}
data.setText( new String(str) );
}
htin.close();
htcon.close();
}
catch( Exception e ) {
// error
}
}
}
}
public class InetAccess extends IApplication {
public void start() {
Display.setCurrent( new NetPanel() );
}
}
モバイルショップ
FEED BACK |