gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:00 26 4
gpt4 key购买 nike

这是我用来验证的代码,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, "File exists!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "File does not exist!", 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;
}

}
}, 0,10000, TimeUnit.MILLISECONDS);
}

关于android - 检查服务器上是否存在 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com