Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 279|回复: 0

[J2ME学习]通过http协议浏览网络文本

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 07:10:09 | 显示全部楼层 |阅读模式
    代码如下:
       

       
    1. [img]http://img.javaxxz.com/2014/10/11/070948281.gif[/img]

    2. import java.io.*;
    3. import javax.microedition.io.*;
    4. import javax.microedition.lcdui.*;
    5. import javax.microedition.midlet.*;
    复制代码

       
      
       
       
       

       
      
      

    1. public class HttpTXT extends MIDlet implements CommandListener {
    2.         private Command exitCommand, loadCommand;
    3.         private Form mainForm;
    4.         private TextField tfItem;
    5.         private TextField tf;
    6.         public HttpTXT() {
    7.                 mainForm = new Form("下载并显示文本文件");
    8.                 tf = new TextField("文本地址", "http://127.0.0.1/test/test.txt", 120,
    9.                                 TextField.URL);
    10.                 //设置控件的布局
    11.                 tf.setLayout(Item.LAYOUT_NEWLINE_AFTER);
    12.                 tfItem = new TextField("文本内容", "", 500,        TextField.ANY);
    13.                 tfItem.setPreferredSize(mainForm.getWidth(), 200);
    14.                 tfItem.setLayout(Item.LAYOUT_VEXPAND | Item.LAYOUT_2);
    15.                 mainForm.append(tf);
    16.                 minForm.append(tfItem);
    17.                 exitCommand = new Command("退出", Command.EXIT, 1);
    18.                 loadCommand = new Command("下载", Command.SCREEN, 1);
    19.                 mainForm.addCommand(exitCommand);
    20.                 mainForm.addCommand(loadCommand);
    21.                 mainForm.setCommandListener(this);
    22.         }
    23.         protected void startApp() throws MIDletStateChangeException {
    24.                 Display.getDisplay(this).setCurrent(mainForm);
    25.         }
    26.         protected void pauseApp() {
    27.         }
    28.         protected void destroyApp(boolean p1) {
    29.         }
    30.         public void commandAction(Command c, Displayable d) {
    31.                 if (c == exitCommand) {
    32.                         destroyApp(false);
    33.                         notifyDestroyed();
    34.                 } else if (c == loadCommand) {
    35.                         //开启一个新的网络连接后台线程
    36.                         new DownLoadTXT(mainForm, tfItem, tf.getString());
    37.                 }
    38.         }
    39.         private class DownLoadTXT extends Thread {
    40.                 private String txtURL;
    41.                 private Form form;
    42.                 private TextField tfItem;
    43.                 public DownLoadTXT(Form f, TextField i, String url) {
    44.                         form = f;
    45.                         tfItem = i;
    46.                         txtURL = url;
    47.                         this.start();
    48.                 }
    49.                 public void run() {
    50.                         String str = null;
    51.                         try {
    52.                                 str = getText(txtURL);
    53.                         } catch (Exception e) {
    54.                                 System.out.println("错误 " + e.getMessage());
    55.                         }
    56.                         form.setTicker(null);
    57.                         if (tfItem != null)                               
    58.                                 tfItem.setString(str);
    59.                 }
    60.                 public String getText(String url) throws IOException, Exception {
    61.                         HttpConnection c = null;
    62.                         InputStream is = null;
    63.                         String str =null;
    64.                         try {
    65.                                 c = (HttpConnection) Connector.open(url);
    66.                                 //判断是否连接成功
    67.                                 int rc = c.getResponseCode();
    68.                                 if (rc != HttpConnection.HTTP_OK) {
    69.                                         throw new Exception(c.getResponseMessage());
    70.                                 }
    71.                                 //获得文本的二进制数据
    72.                                 is = c.openInputStream();
    73.                                
    74.                                 int length = (int) c.getLength();
    75.                                
    76.                                 if (length > 0) {
    77.                                         byte servletData[] = new byte[length];
    78.                                         is.read(servletData);
    79.                                         str = new String(servletData);
    80.                                 }
    81.                                                                
    82.                                
    83.                         } catch (IOException e) {
    84.                                 System.out.println("错误:" + e.getMessage());
    85.                         } finally {
    86.                                 if (is != null)
    87.                                         is.close();
    88.                                 if (c != null)
    89.                                         c.close();
    90.                         }
    91.                         return str;
    92.                 }
    93.         }
    94. }
    95.                      
    复制代码

       
         
         
          
          

            
          

            
          
         
       

    1.                         
    复制代码

      


    源码下载:http://203.93.208.26/kj/cwb/dir7/HttpTXT.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-22 17:44 , Processed in 0.387260 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表