//指定のURLのドキュメントを読み込む
import com.nttdocomo.io.*; //HttpConnection インタフェースの利用により。
import java.io.*;
import com.nttdocomo.ui.*;
import javax.microedition.io.*;
public class GetHTMLDoc extends IApplication {
public void start(){
UrlPanel urlPanel = new UrlPanel(this,"URL入力パネル");
HtmlPanel htmlPanel = new HtmlPanel(this,"HTML TEXT Browser");
urlPanel.setSoft1Frame(null,"EXIT");
urlPanel.setSoft2Frame(htmlPanel,"接続");
htmlPanel.setSoft1Frame(urlPanel,"BACK");
Display.setCurrent(urlPanel);
}
}
//URLを入力するパネル
class UrlPanel extends EditPanel {
public UrlPanel(IApplication app,String title){
super(app,title);
setText("http://www001.upp.so-net.ne.jp/yuu/iappli/helloworld.html");
setSoft1Frame(null,"EXIT");
}
public void soft1NullAction(){
i_app.terminate();
}
public void changeNextEvent(Frame from, Frame to){
EditPanel panel = (EditPanel) to;
String url = this.getText();
panel.ticker.myTicker.setText(url);
panel.setText(HttpUTIL.getHTML(url));//接続文字列を設定
}
}
//HTMLを表示するパネル
class HtmlPanel extends EditPanel {
public HtmlPanel(IApplication app,String title){
super(app,title);
setText("");
this.ticker.dispose(); //今日の表示を停止
}
}
// HTMLを取得するクラス
class HttpUTIL {
public static String getHTML(String url){
StringBuffer sbuf = new StringBuffer();
try{
//引数のurlへ接続
HttpConnection con = (HttpConnection)Connector.open(url,Connector.READ,true);//接続インターフェイス取得
con.setRequestMethod(HttpConnection.GET);//処理の種類を設定("HEAD", "GET", "POST")
con.connect();//HTTP で接続し、メッセージを送受信
InputStream is = con.openInputStream();//入力用ストリームを取得
int rc;
byte buf[] = new byte[512];
while(true){
rc = is.read(buf);
if ( rc <= 0 ) break;
sbuf.append(new String(buf,0,rc));
}
is.close();
con.close();//接続を閉じる。
}
catch(IOException ex){
System.out.println(ex.toString());
}
return sbuf.toString();
}
}