Check if URL exists or not on Server

Sophie picture Sophie · Oct 17, 2014 · Viewed 16k times · Source

This is my code which I am using to verify, URL exists or not on Server, but always getting not exist however link is alive

Where I am doing mistake in my code, why I am always getting "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;
        }
    }

}

Answer

mmlooloo picture mmlooloo · Oct 17, 2014

You will get Network On Main Thread Exception

Look at NetworkOnMainThreadException

so your method always returns false because of:

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

With a ScheduledThreadPoolExecutor:

but remember to shut down 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);
    }