android – 检查服务器上是否存在URL

前端之家收集整理的这篇文章主要介绍了android – 检查服务器上是否存在URL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我用来验证的代码,Url是否存在于服务器上,但始终不存在但链接仍然存在

在我的代码中我犯了错误,为什么我总是“不存在!”

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

}

解决方法

您将获得Network On Main Thread Exception

看看NetworkOnMainThreadException

所以你的方法总是返回false,因为:

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

快速解决

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,Toast.LENGTH_SHORT).show();      
                }
                else
                {           
                    Toast.makeText(MainActivity.this,Toast.LENGTH_SHORT).show();
                }                  
        }           
    }
}

使用ScheduledThreadPoolExecutor:

但记得关闭它!!

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

            }
        },10000,TimeUnit.MILLISECONDS);
    }
原文链接:https://www.f2er.com/android/313900.html

猜你在找的Android相关文章