can I check if a file exists at a URL? - java

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
}

Related

java.net.MalformedURLException in Intellij IDEA [duplicate]

How come this code is giving me a unhandled exception java.net.malformedurlexception in java ?
String u = "http://webapi.com/demo.zip";
URL url = new URL(u);
Can someone tell me how to fix?
You need to handle the posible exception.
Try with this:
try {
String u = "http://webapi.com/demo.zip";
URL url = new URL(u);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Use a try catch statement to handle exceptions:
String u = "http://webapi.com/demo.zip";
try {
URL url = new URL(u);
} catch (MalformedURLException e) {
//do whatever you want to do if you get the exception here
}
java.net.malformedurlexception
It means that no legal protocol could be found in a specification string or the string could not be parsed or your URL is not confirmed the spec or missing a component
I think this will help you to understand URL
https://url.spec.whatwg.org/

DELETE Request in Android doesn't connect to server

So my question is how can I create a DELETE Request to an URL in Android Studio Java. I already have an Async Task which GET json from URL. So my question now is how can I create a DELETE request
EDIT:
So right now I got this code:
int pos = arrlist.get(info.position).getId();
URL_DELETE = "http://testserver/test/tesst.php?id=" + pos + "&username=" + username + "&password=" + password;
URL url = null;
try {
url = new URL(URL_DELETE);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
To understand the content of the given URL should be deleted. But if I run the code nothing happens.
You need to call connect() on the HttpURLConnection. Right now you're not actually making a connection to the server.
Based on your comments on the other answer, you're also trying to run this code on the main (UI) thread - you'll need to change your code to run on a background thread.
If you're using OkHttp:
Request request = new Request.Builder().delete().url(url).build();
Response rawResponse = null;
try {
rawResponse = new OkHttpClient().newCall(request).execute();
} catch (IOException e) {
System.err.println(e.getMessage());
}
String responseAsString = rawResponse.body().string();

Facebook Graph API request returning IOException "Hostname <fbcdn-profile-a.akamaihd.net> was not verified"

So I'm trying to simply fetch the user's profile photo from facebook but I'm getting a null response from facebook.request(path) and the IOException "Hostname fbcdn-profile-a.akamaihd.net was not verified".
Anyone know what could be causing this exception? Here's my method to call the facebook.request:
public Bitmap getUserPic(String path){
URL picURL = null;
try {
responsePic = facebook.request(path);
picURL = new URL(responsePic);
HttpURLConnection conn = (HttpURLConnection)picURL.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
userPic = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userPic;
}
The string "path" is "me/picture"
Edit:
Also tried setting picURL to "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/260885_608260639_822979518_q.jpg" which is the url that the request should return. Still no photo :(
Thanks for any help
It sounds like a issue with the HTTPS connection used to get the image from the Facebook CDN. What happens if you request the regular HTTP version of the image?
E.g. http://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/260885_608260639_822979518_q.jpg

Open stream from uri

I got an uri (java.net.URI) such as http://www.example.com. How do I open it as a stream in Java?
Do I really have to use the URL class instead?
You will have to create a new URL object and then open stream on the URL instance. An example is below.
try {
URL url = uri.toURL(); //get URL from your uri object
InputStream stream = url.openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
URLConnection connection = uri.toURL().openConnection()
Yes, you have to use the URL class in one way or the other.
You should use ContentResolver to obtain InputStream:
InputStream is = getContentResolver().openInputStream(uri);
Code is valid inside Activity object scope.
uri.toURL().openStream() or uri.toURL().openConnection().getInputStream()
You can use URLConnection to read data for given URL. - URLConnection

Check if URL is valid in Android without catching Exception?

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;
}
}

Categories