在我的代码中我犯了错误,为什么我总是“不存在!”
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); }总结
以上是内存溢出为你收集整理的android – 检查服务器上是否存在URL全部内容,希望文章能够帮你解决android – 检查服务器上是否存在URL所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)