2014-10-17 15 views
5

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

} 
+0

http://stackoverflow.com/questions/1378199/how-to-check-if-a-url-exists-or-returns-404-with-java –

+0

@NaveenTamrakar versucht immer noch zu bekommen "existiert nicht!" – Sophie

+0

entfernen Sie diese in der vorhandenen Funktion HttpURLConnection.setFollowRedirects (false); – ashutiwari4

Antwort

12

existiert, wird Sie Network On Main Thread Exception

Schauen Sie erhalten bei NetworkOnMainThreadException

so Ihre Methode immer false zurückgibt, weil:

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

schnelle Lösung:

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

Mit einem ScheduledThreadPoolExecutor:

aber denken Sie daran, es herunterzufahren !!

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); 
    } 
+0

vielen Dank können Sie eine kleine Änderung in bestehenden Code für mich tun, weil ich nach diesem Link in allen 10 Sekunden zu überprüfen, also können Sie Änderungen für diese – Sophie

+0

Schreiber-Timer für diese Aufgabe vornehmen. es wird diese asytask jedesmal @Sophie – Venu

+0

nennen Sie können es ohne asynctask wieder schauen! – mmlooloo

1

ändern () dieser

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

versuchte immer noch bekommen "existiert nicht!" – Sophie

0

können Sie den Code folgen, um zu versuchen verwenden.

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, 

Wenn Sie dies tun, wird es Can Exception geben und Sie können nicht das richtige Ergebnis erhalten. Wenn Sie also möchten, dass die Anwendung eine Netzwerkoperation ausführt, können Sie einen anderen Thread dazu verwenden.

0

Verwenden if(bResponse) statt if(bResponse==true)

Verwandte Themen