Dies ist mein Code, die ich zu überprüfen, verwende, URL vorhanden ist oder nicht auf dem Server, aber immer nicht immer existieren jedoch Link lebtÜberprüfen Sie, ob URL existiert oder nicht auf Server
Wo ich Fehler in meinem Code tue Warum bekomme ich immer "doesnot exist!"
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String customURL = "http://www.desicomments.com/dc3/08/273858/273858.jpg";
boolean bResponse = exists(customURL);
if (bResponse==true)
{
Toast.makeText(MainActivity.this, "File exists!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "File does not exist!", Toast.LENGTH_SHORT).show();
}
}
public static boolean exists(String URLName){
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
http://stackoverflow.com/questions/1378199/how-to-check-if-a-url-exists-or-returns-404-with-java –
@NaveenTamrakar versucht immer noch zu bekommen "existiert nicht!" – Sophie
entfernen Sie diese in der vorhandenen Funktion HttpURLConnection.setFollowRedirects (false); – ashutiwari4