雷老虎好:
    下面是源代码,用于读取一个txt文件的第一行,单个运行时可以读出。但用JAD启动时就出现Application Error:IILegalArgument 这一句是出现在手机中,用的环境是The JBuilder MobileSet, Nokia Edition。经查出错语句是openInputStream。烦请仔细看一下,关系到我的前途问题,我会考虑加分的。谢谢!package testmidp;import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;public class firstExample extends MIDlet {
  private Display display;
  String url="http://localhost/hello.txt";
  //String url=this.getAppProperty("url");
  /**Construct the midlet*/
  public firstExample() {
 display=Display.getDisplay(this);
  }  /**Main method*/
  public void startApp() {
 try
 {
    this.getViaStreamConnection(url);
 }
 catch(IOException e)
 {
    System.out.println("IOException: " + e);
    e.printStackTrace();
 }
  }  /**Handle pausing the MIDlet*/
  public void pauseApp() {
  }  /**Handle destroying the MIDlet*/
  public void destroyApp(boolean unconditional) {
  }  void getViaStreamConnection(String url) throws IOException
  {
    StreamConnection c=null;
    InputStream s=null;
    StringBuffer b=new StringBuffer();
    TextBox t=null;
    try
    {
      c=(StreamConnection)Connector.open(url);
      s=c.openInputStream();    //出错语句
      int ch;
      while((ch=s.read()) != 13)
      {
b.append((char)ch);
      }
      System.out.println(b.toString());
      t=new TextBox(url,b.toString(),1024,0);
    }
    finally
    {
      if(s!=null)
s.close();
      if(c!=null)
c.close();
    }
    display.setCurrent(t);
  }
}

解决方案 »

  1.   

    用这个试试,我的环境跟你不一样,在命令行、wireless toolkit可以执行,不知你所说的单个文件运行是怎么回事,不用jad吗,可否看看你的jad?
    我这里使用了InputStreamReader,InputStream是一个抽象类,而StreamConnection是接口,我怀疑会有问题。
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.io.*;
    import javax.microedition.io.*;public class help extends MIDlet {
      private Display display;
      String url="http://localhost/hello.txt";
      //String url=this.getAppProperty("url");
      /**Construct the midlet*/
         public help() {
    display=Display.getDisplay(this);
      }  /**Main method*/
      public void startApp() {
    try
    {
        this.getViaStreamConnection(url);
    }
    catch(IOException e)
    {
        System.out.println("IOException: " + e);
        e.printStackTrace();
    }
      }  /**Handle pausing the MIDlet*/
      public void pauseApp() {
      }  /**Handle destroying the MIDlet*/
      public void destroyApp(boolean unconditional) {
      }  void getViaStreamConnection(String myurl) throws IOException
      {
        
        InputStreamReader isr=null;
        StringBuffer b=new StringBuffer();
        TextBox t=null;
        try
        {
           isr =new InputStreamReader(Connector.openInputStream(myurl));
          int ch;
          while((ch=isr.read()) != 13)
          {
    b.append((char)ch);
          }
          System.out.println(b.toString());
          t=new TextBox(url,b.toString(),1024,0);
        }
        finally
        {
          if(isr!=null)
    isr.close();
          
        }
        display.setCurrent(t);
      }
      

  2.   

    雷老虎:我按你的代码改过了,单独运行时可以达到结果,但通过JAD运行时就不行了,错误一样。附我的JAD文件(以前我想用其中的URL,但现在根本没用,只是想把几个放在一起而已),请你尽快想想办法吧,谢谢你了,等好消息:MIDlet-Name: connection
    MIDlet-Vendor: fengrain
    MIDlet-Version: 1.0
    MIDlet-Jar-Size: 4687
    MIDlet-Jar-URL: connection.jar
    MIDlet-1: firstExample, , testmidp.firstExample
    MIDlet-2: secondExapmle, , testmidp.secondExapmle
    MIDlet-3: testMidlet, , testmidp.testMidlet
    url: http://localhost:8080/test/Hello.txt
      

  3.   

    sorry,我对这种开发环境不熟悉,会不会是开发环境的问题呢?
    按照jad的功能,作为变量的url怎么会出现在jad中呢?
    我也在MIDlet suite中加了几个MIDlet,不论是放在网上用midp -install 或在本地用midp -descriptor 都没问题,在命令行、j2me wireless toolkit ]forte 2.0 ce中执行也都没问题,如果nokia的开发环境允许jad中定义变量,那么我怀疑问题在url的定义上: :8080是必须的吗?
      

  4.   

    8080是必须的,我也可以通过IE访问这个servlet。url定义是可行的,我通过其它的例子试过了,可以读出来。我再试试用j2me wireless toolkit吧。希望你能留下EMAIL,以后可能还要向你虚心请教的。先给你加分了。
      

  5.   

    请教可不敢当,我还处在研究j2me的源码阶段,没有编过几个MIDlet,我觉得用各个公司的开发包可能会妨碍对j2me本身的了解,当然,如果是专为某个公司或测试MIDlet的可移植性就不一样了。
    E-mail:[email protected]