Check it out, it works for me. Source URL: Check if the URL exists on the server
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(); } } } }
Ishtiyaq Husain Nov 03 '15 at 9:56 2015-11-03 09:56
source share