Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    Problem with Download an Image from an HttpServer with NOKIA 7650 and NOKIA 6350

    Hello,
    I try to test an example for download an Image from an HttpServer, the example work fine in emulator but in NOKIA7650 or Nokia 3650 i have this exception:ERRUER D'application:NegativeArraySize.

    Can you help me
    THANKS.

    the code is:
    import java.io.*;
    import java.util.*;

    import javax.microedition.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;

    public class LogoLoader
    extends MIDlet {
    private Item mItem;
    private Vector LogoUrls = new Vector();
    private Random random = new Random ();
    private int NumUrls = 0;

    public LogoLoader() {
    LogoUrls.addElement("http://www.comp.polyu.edu.hk/~csstlee/PolyU.png");
    LogoUrls.addElement("http://www.comp.polyu.edu.hk/~csstlee/CU.png");
    LogoUrls.addElement("http://www.comp.polyu.edu.hk/~csstlee/HKU.png");
    NumUrls = LogoUrls.size();
    }


    public void startApp() {
    final Display display = Display.getDisplay(this);

    if (mItem == null) {
    // Put up loading progress screen.
    Form progressForm = new Form("Loading");
    display.setCurrent(progressForm);

    // Create the Form that will show the Image Logo.
    final Form imageForm = new Form("Logo");
    imageForm.addCommand(new Command("Exit", Command.EXIT, 0));
    imageForm.setCommandListener(new CommandListener() {
    public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
    }
    });

    // Do network loading in a separate thread.
    Thread t = new Thread() {
    public void run() {
    try {
    int LogoIndex = Math.abs(random.nextInt()) % NumUrls;
    String LogoUrl = (String)LogoUrls.elementAt(LogoIndex);
    Image image = loadImage(LogoUrl);
    mItem = new ImageItem(null, image, 0, null);
    }
    catch (IOException ioe) {
    mItem = new StringItem(null, ioe.toString());
    }
    imageForm.append(mItem);
    display.setCurrent(imageForm);
    }
    };
    t.start();
    }

    }

    public Image loadImage(String url) throws IOException {
    HttpConnection hc = (HttpConnection)Connector.open(url);

    try {
    int length = (int)hc.getLength();
    byte[] data = new byte[length];
    DataInputStream in = new DataInputStream(hc.openInputStream());
    in.readFully(data);
    return Image.createImage(data, 0, data.length);
    }
    finally {
    hc.close();
    }
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    10
    I assume that the problem is in that
    int length = (int)hc.getLength();
    It returns -1 which means that the 'content-length' field dosn't have value or is unknown. So the next line gives you the error ...
    byte[] data = new byte[length];

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×