With the following code, the server is getting hit, but I also want to send variables in POST method.
But it just gives me a null pointer exception. It has something to do with the OutputStream
. I have checked postData
... it has some address, which means it is not null.
So why I am given null pointer exception?
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
try {
connDesc = connFact.getConnection("http://example.com/login.php", Connector.READ_WRITE, returnContent);
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("count", "Hell Yeah!");
byte[] postData = encPostData.toString().getBytes("UTF-8");
httpConn.setRequestProperty("Content-length", String.valueOf(postData.length));
Dialog.alert("Post Data: " + postData);
OutputStream os = httpConn.openOutputStream();
os.write(postData);
os.flush();
httpConn = (HttpConnection) connDesc.getConnection();
final int iResponseCode = httpConn.getResponseCode();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//Dialog.alert("Response code: " + Integer.toString(iResponseCode));
}
});
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}