检查URL服务器上存在与否 [英] Check if URL exists or not on Server
本文介绍了检查URL服务器上存在与否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的code这我使用验证,URL的存在与否服务器上,但总是越来越但是不存在链路是活的。
在哪里我做的错误在我的code,为什么我总是收到不列入存在!
公共类MainActivity延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 字符串customURL =http://www.desicomments.com/dc3/08/273858/273858.jpg;
布尔bResponse =存在(customURL); 如果(bResponse ==真)
{
Toast.makeText(MainActivity.this!文件存在,Toast.LENGTH_SHORT).show();
}
其他
{
Toast.makeText(MainActivity.this!文件不存在,Toast.LENGTH_SHORT).show();
} } 公共静态布尔存在(字符串URLNAME){
尝试{
HttpURLConnection.setFollowRedirects(假);
HttpURLConnection的CON =(HttpURLConnection类)新的URL(URLNAME).openConnection();
con.setRequestMethod(HEAD);
回报(con.getResponse code()== HttpURLConnection.HTTP_OK);
}
赶上(例外五){
e.printStackTrace();
返回false;
}
}}
解决方案
您将获得网络在主线程异常
在 NetworkOnMainThreadException
看所以你的方法总是返回,因为错误的:
赶上(例外五){
e.printStackTrace();
返回false;
}
快速修复:
公共类MainActivity延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 字符串customURL =http://www.desicomments.com/dc3/08/273858/273858.jpg; MyTask任务=新MyTask();
task.execute(customURL);
}
私有类MyTask扩展的AsyncTask<弦乐,太虚,布尔> { @覆盖
在preExecute保护无效(){ } @覆盖
保护布尔doInBackground(字符串... PARAMS){ 尝试{
HttpURLConnection.setFollowRedirects(假);
HttpURLConnection的CON =(HttpURLConnection类)新的URL(PARAMS [0])的openConnection()。
con.setRequestMethod(HEAD);
的System.out.println(con.getResponse code());
回报(con.getResponse code()== HttpURLConnection.HTTP_OK);
}
赶上(例外五){
e.printStackTrace();
返回false;
}
} @覆盖
保护无效onPostExecute(布尔结果){
布尔bResponse =结果;
如果(bResponse ==真)
{
Toast.makeText(MainActivity.this!文件存在,Toast.LENGTH_SHORT).show();
}
其他
{
Toast.makeText(MainActivity.this!文件不存在,Toast.LENGTH_SHORT).show();
}
}
}
}
随着的ScheduledThreadPoolExecutor:
但记得要下来关闭它!
公共类MainActivity延伸活动{
串customURL;
弦乐味精=;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); customURL =http://www.desicomments.com/dc3/08/273858/273858.jpg; 最终的ScheduledThreadPoolExecutor myTimer =新的ScheduledThreadPoolExecutor(1);
myTimer.scheduleAtFixedRate(新的Runnable(){ @覆盖
公共无效的run(){ 尝试{
HttpURLConnection.setFollowRedirects(假);
HttpURLConnection的CON =(HttpURLConnection类)新的URL(customURL).openConnection();
con.setRequestMethod(HEAD);
的System.out.println(con.getResponse code()); 如果(con.getResponse code()== HttpURLConnection.HTTP_OK){ 味精=文件存在! }其他{ 味精=文件不存在! } runOnUiThread(新的Runnable(){ @覆盖
公共无效的run(){ Toast.makeText(MainActivity.this,味精,Toast.LENGTH_SHORT).show();
}
});
}
赶上(例外五){
e.printStackTrace();
返回;
} }
},0,10000,TimeUnit.MILLISECONDS);
}
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;
}
}
}
解决方案
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);
}
这篇关于检查URL服务器上存在与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文