Android 如果服务器关闭,如何避免Jsoup和Asynctask崩溃

Android 如果服务器关闭,如何避免Jsoup和Asynctask崩溃,android,crash,android-asynctask,jsoup,Android,Crash,Android Asynctask,Jsoup,我需要一些帮助。 我开发了一个解析网站的应用程序。 现在一切都很好(也要感谢你)。但是当我想要解析的站点关闭时,我面临一个问题。 应用程序刚刚崩溃。。。我试着改善超时连接,当站点速度很慢的时候,这种方法就行了。但我如何管理服务器关闭错误? 我想在文本视图中打印一个类似的错误。 这是我代码的一部分 String result = ""; Document doc = null; try {

我需要一些帮助。 我开发了一个解析网站的应用程序。 现在一切都很好(也要感谢你)。但是当我想要解析的站点关闭时,我面临一个问题。 应用程序刚刚崩溃。。。我试着改善超时连接,当站点速度很慢的时候,这种方法就行了。但我如何管理服务器关闭错误? 我想在文本视图中打印一个类似的错误。 这是我代码的一部分

String result = "";
                Document doc = null;
                try {
                      Connection conn = Jsoup.connect(BLOG_URL).timeout(14000);
                        doc = conn.get();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

在服务器关闭时处理崩溃,如下所示

private class doSomethingDelayed extends AsyncTask<Void, Integer, Void> {

    private int num_runs = 0;

    @Override
    protected Void doInBackground(Void... gurk) {

        try {
                   //stuffs...
         publishProgress(num_runs);
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
        serviceAvailable = true;                    
    } catch (Exception e) {
        e.printStackTrace();
    }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (serviceAvailable == true) {
            serviceAvailable = false;
            Toast.makeText(getApplicationContext(), "Service not available", Toast.LENGTH_SHORT).show();
            System.out.println("in onPostExecute method --");
        }
    }

    @Override
    protected void onProgressUpdate(Integer... num_runs) {
        try {

        } catch (JSONException e) {
            e.printStackTrace();

        }
    }
}
私有类doSomethingDelayed扩展异步任务{
私有int num_runs=0;
@凌驾
受保护的Void doInBackground(Void…gurk){
试一试{
//东西。。。
出版进度(运行次数);
}捕获(客户端协议例外e){
}捕获(IOE异常){
serviceAvailable=true;
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
如果(serviceAvailable==true){
serviceAvailable=false;
Toast.makeText(getApplicationContext(),“服务不可用”,Toast.LENGTH_SHORT.show();
System.out.println(“in-onPostExecute方法--”);
}
}
@凌驾
受保护的void onProgressUpdate(整数…num_运行){
试一试{
}捕获(JSONException e){
e、 printStackTrace();
}
}
}

在async onPostExecute(..)中处理它,但在我的日志中,我看到DoInBackgroundyup中崩溃了。。您可以处理thr本身。如果需要serviceAvailable,为什么不直接使用返回值呢?在超过超时的情况下是否可以工作?因为我仍然崩溃时,网站太慢,服务器并没有关闭。对不起,我是Android新手。谢谢lot@triggs--serviceAvailable只是一个布尔值。您可以返回值。@KriMar这对我来说很好。我也在用这个做服务器相关的项目。