Using java to communicate with a PHP script on my server
I need to be able to access my database, which in it's own isn't hard as java can directly access it. I want to, however, use a php script to access it, as I need to insert stuff into the database as well, and开发者_Go百科 I don't want to have the username and password of a read-write accoutn for my database in my java code. If someone decompiles it, he can just access my database and do stuff with it...
So basicly, I want to use a PHP script and send $_POST request info from my java code to my php script
(yes, java, not javascript ;-) )
You can do this, no problem. Use a URLConnection with setDoOutput(true)
, and get its output stream for the POST.
Of course, your PHP script should make sure that all data which is sent is sane, as anyone could send such a request to your PHP script. (Or you would need some way for the Java application to authenticate itself to the PHP script, which simply shifts the problem of hiding these credentials instead of the database ones.)
You should store DB credentials in configuration file to avoid embedding them directly into the code.
精彩评论