I have some code that needs to form a connection to a server, and once that connection is formed the server interprets the string that is sent and updates the names in a database. I.e. I send "http://app.sitename.tv/api/add/aName" and it adds "aName" to the database, or i send "http://app.sitename.tv/api/remove/aName" and it removes the name.
When I type these links into a browser and run it it works fine. From my program, though, it doesn't throw any exceptions but does not update the database. I'm not sure where to go from here. I've tried turning off my firewall but it makes no difference.
if ((Boolean) value)
{
System.out.println("setValue");
try
{
String bvURL = "http://app.sitename.tv/api/add/" + data.name.get(row);
URL myURL = new URL(bvURL);
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
System.out.println(bvURL);
}
catch (MalformedURLException e)
{
System.err.println("MalformedURLException: " + e);
}
catch (IOException e)
{
System.err.println("IOException: " + e);
}
}
else if ((Boolean) value == false)
{
System.out.println("setValue");
try
{
String bvURL = "http://app.sitename.tv/api/remove/" + data.name.get(row);
URL myURL = new URL(bvURL);
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
System.out.println(bvURL);
}
catch (MalformedURLException e)
{
System.err.println("MalformedURLException: " + e);
}
catch (IOException e)
{
System.err.println("IOException: " + e);
}
}
The
URLConnection
you are getting is actually an HttpURLConnection. You have opened the connection but not closed it. To request that theHttpURLConnection
be closed, you should call its disconnect() method. It could be that the connection needs to be closed in order to ensure that the server completely receives your request and processes it.For example, you can replace your first try/catch block with this:
If this still does not work, you may need to get the
InputStream
from the connection, usinggetInputStream()
, and consume all the input data before disconnecting.If you don't check the response code or get the input or output streams, there is no connection at all. All you've done by calling connect() is create an HttpURLConnection object in your own JVM. Nothing more, and specifically nothing on the network whatsoever.