views:

120

answers:

2

Ok, so I'm using Java ME to connect to a PHP script on my server, but for some odd reason the networking will only work the first time the app is run on the phone after installation. Any other time, it won't even ask permission to use the network. Here's the method in question:

private static String defaultURL = "http://www.roostercogburn.co.uk/proj/uploadScript.php";


public static String upload(String requeststring) {
    HttpConnection hc = null;
    DataInputStream dis = null;
    DataOutputStream dos = null;
    StringBuffer messagebuffer = new StringBuffer();
    requeststring = "xml=" + requeststring;
    try {
        // Open up a http connection with the Web server
        // for both send and receive operations
        hc = (HttpConnection) Connector.open(defaultURL, Connector.READ_WRITE);
        // Set the request method to POST
        hc.setRequestMethod(HttpConnection.POST);
        // Send the string entered by user byte by byte
        dos = hc.openDataOutputStream();
        byte[] request_body = requeststring.getBytes();
        for (int i = 0; i < request_body.length; i++) {
            dos.writeByte(request_body[i]);
        }
        dos.flush();
        dos.close();
        // Retrieve the response back from the servlet
        dis = new DataInputStream(hc.openInputStream());
        int ch;
        // Check the Content-Length first
        long len = hc.getLength();
        if (len != -1) {
            for (int i = 0; i < len; i++) {
                if ((ch = dis.read()) != -1) {
                    messagebuffer.append((char) ch);
                }
            }
        } else {
            // if the content-length is not available
            while ((ch = dis.read()) != -1) {
                messagebuffer.append((char) ch);
            }
        }

        dis.close();

        hc.close();
    } catch (IOException ioe) {
        messagebuffer = new StringBuffer("ERROR!");
    } finally {
        // Free up i/o streams and http connection
        try {
            if (hc != null) {
                hc.close();
            }
        } catch (IOException ignored) {
        }
        try {
            if (dis != null) {
                dis.close();
            }
        } catch (IOException ignored) {
        }
        try {
            if (dos != null) {
                dos.close();
            }
        } catch (IOException ignored) {
        }
    }
    return messagebuffer.toString();
}

Any ideas?

A: 

For me this code seems not to be the problem. Created a quick test MIDlet and everything worked on emulator and SonyEricsson W960.

My test form:

class TestForm extends Form implements CommandListener, Runnable {
  private final Command exit = new Command("Exit", Command.EXIT, 0);
  private final Command run = new Command("Run", Command.OK, 0);
  private int testRunCount;

  public TestForm() {
    super("Test");
    addCommand(exit);
    addCommand(run);
    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == exit) {
      notifyDestroyed();
    } else if (cmd == run) {
      final Thread t = new Thread(this);
      t.start();
    }
  }

  public void run() {
    testRunCount++;
    append("Test run " + testRunCount + "\n");
    final String result = upload("StackOverflow testrun " + testRunCount);
    append("Result > " + result + "\n");
  }
}

BTW:

Any other time, it won't even ask permission to use the network

There's no requirement to ask every time. On phones/emulators usually permission is asked only once per application run.

Check that this method is even called after initial calling :)

/JaanusSiim

JaanusSiim
By any other time, I mean I exit the app, and then when I open it again, it won't do any networking stuff. Thanks for having a look at it though. If it's not this code, it must be elsewhere the problem lies. Thanks
A: 

The use of "static" reminds me that JavaME virtual machines usually don't unload classes.

I know that at least the Series40 platform leaves the VM process running all the time so static variables aren't reset and static initialisation code is only run once.

Try to switch the phone off and on to make sure you're not running into this issue.

Otherwise, maybe post the whole code of a simple MIDlet you use this code into and tell us what handsets you have tried it on.

You can also use logging (file or standard output) to make sure your upload method is actually running the second time (or figure out why it doesn't)

QuickRecipesOnSymbianOS