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?