• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java connection to a URL

I'm trying to pass data from a desktop application to a php page for storage in a MySQL database. Currently, I'm forming the URL with GET parameters, and capturing those in the php file. I can't get Java to connect to the page and pass the data. I've also tried using the OutputStreamWriter and sending the variables via POST, but I can't seem to get anything to work.

I know the php script is functioning properly because when I manually visit the page in a browser, it works fine.

Edit: I went back to the post method, still no luck. View the code, since the code editor here isn't working.
 
That's interesting. I'd suggest doing two things:
1. Make sure connection is an "instanceof" HttpURLConnection.
2. If it is, cast it to that, and print the getResponseCode() and getResponseMessage().

P.S. Code blocks don't work well yet. You won't get them to wrap AFAIK.
 
When I printed the getResponseMessage() and getResponseCode(), it finally worked. It seems, atleast in this case, that I have to call those methods for this to work. Not exactly sure why, but I'm not complaining. 😀

Final Code
 
I guess the HttpURLConnection needs a stimulus to actually send a request. Given that, you should only need one of getResponseCode() or getResponseMessage(). I'm not sure which would be faster; probably the Message, since the Code has to be extracted from the string.
 
Back
Top