Check if url exists on server

This is my code that I use to verify that the URL exists or does not exist on the server, but always becomes inaccessible, although the link is active

Where I make a mistake in my code, why I always get "does not 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; } } } 
+6
android url thread-safety android-asynctask file-exists
Oct 17 '14 at 5:34
source share
4 answers

You will get Network On Main Thread Exception

See NetworkOnMainThreadException

therefore your method always returns false due to:

  catch (Exception e) { e.printStackTrace(); return false; } 

quick fix:

 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"; MyTask task = new MyTask(); task.execute(customURL); } private class MyTask extends AsyncTask<String, Void, Boolean> { @Override protected void onPreExecute() { } @Override protected Boolean doInBackground(String... params) { try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(params[0]).openConnection(); con.setRequestMethod("HEAD"); System.out.println(con.getResponseCode()); return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { e.printStackTrace(); return false; } } @Override protected void onPostExecute(Boolean result) { boolean bResponse = result; 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(); } } } } 

Using ScheduledThreadPoolExecutor:

but don't forget to close it!

 public class MainActivity extends Activity { String customURL; String msg = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); customURL = "http://www.desicomments.com/dc3/08/273858/273858.jpg"; final ScheduledThreadPoolExecutor myTimer = new ScheduledThreadPoolExecutor(1); myTimer.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(customURL).openConnection(); con.setRequestMethod("HEAD"); System.out.println(con.getResponseCode()); if(con.getResponseCode() == HttpURLConnection.HTTP_OK){ msg = "File exist!"; }else{ msg = "File does not exist!"; } runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { e.printStackTrace(); return; } } }, 0,10000, TimeUnit.MILLISECONDS); } 
+14
Oct 17 '14 at 6:16
source share

Change exists () to

 public boolean exists(String url){ HttpURLConnection huc = ( HttpURLConnection ) url.openConnection (); huc.setRequestMethod ("GET"); //OR huc.setRequestMethod ("HEAD"); huc.connect () ; int code = huc.getResponseCode() ; System.out.println(code); if(code==200) return true; else return false; } 
+1
Oct 17 '14 at 5:42 on
source share

You can use the following code to try.

  final String customURL = "http://www.desicomments.com/dc3/08/273858/273858.jpg"; new Thread(){ @Override public void run() { // TODO Auto-generated method stub super.run(); try { URL url = new URL(customURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("HEAD"); con.connect(); Log.i(TAG, "con.getResponseCode() IS : " + con.getResponseCode()); if(con.getResponseCode() == HttpURLConnection.HTTP_OK){ Log.i(TAG, "Sucess"); } } catch (Exception e) { e.printStackTrace(); Log.i(TAG, "fail"); } } }.start(); Reason: After android 2.3, you can't perform a networking operation on its main thread, 

if you do, there will be an exception, and you will not be able to get the correct result. Therefore, if you want the application to perform a network operation, you can use a different thread for this.

0
Oct 17 '14 at 6:36
source share

Use if(bResponse) instead of if(bResponse==true)

0
07 Feb '17 at 5:28
source share



All Articles