See, I have to check like 50+ URLs for validity, and I'm assuming that catching more than 50 exceptions is kind of over the top. Is there a way to check if a bunch of URLs are valid without wrapping it in a try catch to catch exceptions? Also, just fyi, in Android the class "UrlValidator" doesn't exist (but it does exist in the standard java), and there's UrlUtil.isValidUrl(String url) but that method seems to be pleased with whatever you throw at it as long as it contains http://... any suggestions?
This solution does catch exceptions, however others may find it useful and doesn't require any libraries.
public boolean URLIsReachable(String urlString)
{
try
{
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
responseCode = urlConnection.getResponseCode();
urlConnection.disconnect();
return responseCode != 200;
} catch (MalformedURLException e)
{
e.printStackTrace();
return false;
} catch (IOException e)
{
e.printStackTrace();
return false;
}
}
Related
In the logcat I get a warning:
W/System: A resource failed to call end.
I am 100% positive that this piece of code makes the warning, since when I take it out it stops.
I can't seem to fix it so it doesn't display warning.
The purpose of the code is to check if there is internet connection or not.
It is on separate thread. Declared with:
public class ConnectWifiThread extends Thread {
public static boolean isInternetAvailable(Context context) {
Here is the code:
try {
URL url = new URL("https://www.google.com/");
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setRequestProperty("User-Agent", "test");
https.setRequestProperty("Connection", "close");
https.setConnectTimeout(2000); // mTimeout is in seconds
https.connect();
int tempResponse = https.getResponseCode();
if (tempResponse == 200) {
https.disconnect();
Thread.sleep(50);
https=null;
url=null;
Thread.sleep(50);
Log.d("Has", "internet");
return true;
} else {
https.disconnect();
Thread.sleep(50);
https=null;
url=null;
Thread.sleep(50);
Log.d("NO", "internet");
return false;
}
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
catch (Exception e) {
Log.d("Error checking internet", e.getMessage());
return false;
}
Thank you
When you use a URLConnection, by default it makes an input stream for you to read the response. You'd get that by calling getInputStream() on the connection, and then you'd read the stream to completion and close it.
If you don't need the data, you can alternatively call setDoInput(false) to save you the trouble of doing the above.
In my project i am verifying the links for few landing pages from an excel sheet using Selenium and Java. Below is the code i am using:
public static void verifyConnection(String linkUrl) throws IOException {
URL url = new URL(linkUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
if (urlConnection.getResponseCode() == 200) {
System.out.println("resp 200");
} else {
System.out.println("resp error");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
The problem here is, when the linkUrl is something like https://www.google.com/ the response works fine, but when i do the same for Spanish version of our site with links like https://es.google.com/ or https://espanol.google.com/ i get Connection timed out exception.
Could someone advise, what i am missing?
Note: The above google links may not be valid, it is just to explain my scenario.
Upon trying to connect to a host/server that does not exist, my program just seems to die. Stepping through with the debugger lends me nothing, it makes it to getResponseCode() and then just stops working. No exceptions are thrown from what I can tell and the program doesn't return.
Here is the relevant code snippet:
try {
//construct a URL and open the connection
URL url = new URL("http://" + serverHost + ":" + serverPort + urlSuffix);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
if(http.getResponseCode() != 200) {
System.out.println("Could not connect");
}
System.out.println("Connected");
return;
} catch (MalformedURLException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
} catch (Exception e) { //give me something please
e.printStackTrace();
return;
}
When connecting to a valid URL, it works fine.
I fixed it, I needed to add http.setConnectTimeout(10000); after I opened the connection. Apparently whatever the default timeout was set as was so long that it appeared to never give a response at all, even when leaving the application open for several minutes.
I'm building a project in which I want a method to make a simple http GET request in order to send two variables to an website via URL.
In a normal java project I would likely use java.net or apache and solve the issue in a matter of minutes. In JavaME, due to my lack of experience I'm not really being able to fulfill the task.
Basically what I want to do is having an url like google.com/index.php?v1=x&v=y
being able to do a get request in order to send those variables via URL.
Any tips?
Here's an example of how you could do something like that.
HttpConnection connection = null;
InputStream inputstream = null;
String url = null;
StringBuffer dataReceived = null;
url = "http://www.google.com/index.php?v1=x&v=y";
dataReceived = new StringBuffer();
try {
connection = (HttpConnection) Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("Connection", "close");
if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
inputstream = connection.openInputStream();
int ch;
while ((ch = inputstream.read()) != -1 ) {
dataReceived.append((char) ch);
}
} else {
// Connection not ok
}
} catch (Exception e) {
// Something went wrong
} finally {
if (inputstream != null) {
try {
inputstream.close();
} catch (Exception e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
}
}
}
Note: I didn't test this specific code. I just edited some code I had lying around from a previous project of mine, so you may need to fix a few errors.
can I check if a file exists at a URL?
This link is very good for C#, what about java. I serach but i did not find good solution.
It's quite similar in Java. You just need to evaluate the HTTP Response code:
final URL url = new URL("http://some.where/file.html");
url.openConnection().getResponseCode();
A more complete example can be found here.
Contributing a clean version that's easier to copy and paste.
try {
final URL url = new URL("http://your/url");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
int responseCode = huc.getResponseCode();
// Handle response code here...
} catch (UnknownHostException uhe) {
// Handle exceptions as necessary
} catch (FileNotFoundException fnfe) {
// Handle exceptions as necessary
} catch (Exception e) {
// Handle exceptions as necessary
}