java - 我遇到 AsyncTask 类的问题

标签 java android performance android-asynctask jsoup

当链接为空时,我的应用程序崩溃。我正在尝试下载 Instagram 视频。这些应用程序可以与公共(public)链接配合使用。但我放了一个私有(private)链接,应用程序崩溃了。这意味着使用私有(private)链接,应用程序不会从网站获取 HTML。并且下载链接仍然是空的。这会导致应用程序崩溃。我希望当输入私有(private)链接时应用程序不会崩溃

我尝试在 onPostExecute 方法中使用 if-else 语句,但它不起作用,或者可能是我的方法错误。

这个AsyncTask类

private class InstaVideo extends AsyncTask<Void, Void, Void> {

        String dlink, imglink;

        @Override
        protected void onPreExecute() {
            ProgressBar progressBar = (ProgressBar) findViewById(R.id.loading_indicator);
            progressBar.setVisibility(View.VISIBLE);
            DoubleBounce doubleBounce = new DoubleBounce();
            progressBar.setIndeterminateDrawable(doubleBounce);
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                Document doc = Jsoup.connect("https://www.10insta.net/#grid-gallery")
                        .data("url", temp)
                        .post();
                Log.v("Hello", doc.title());
                Element srctag = doc.select("img.card-img-top").first();
                Element ptag = doc.select("p.card-text").first();
                Element atag = ptag.select("a").first();
                imglink = srctag.attr("src");
                dlink = "https://www.10insta.net/";
                dlink += atag.attr("href");
                Log.i("DownloadActivity",dlink);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (dlink=="https://www.10insta.net/download.php?url=") {
                Toast.makeText(DownloadActivity.this,"Link is private",Toast.LENGTH_SHORT).show();

            } else {
                View loadingIndicator = findViewById(R.id.loading_indicator);
                loadingIndicator.setVisibility(View.GONE);
                TextView t = (TextView) findViewById(R.id.imgtxt);
                if (Content.id == 2) {
                    t.setText("Video Preview");
                } else {
                    t.setText("Image Preview");
                }
                t.setVisibility(View.VISIBLE);
                Button b = (Button) findViewById(R.id.instadownload);
                b.setVisibility(View.VISIBLE);
                final ImageView img = (ImageView) findViewById(R.id.instaimg);
                Picasso.get().load(imglink).placeholder(R.drawable.loading).into(img);
                b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        Uri uri = Uri.parse(dlink);
                        DownloadManager.Request req = new DownloadManager.Request(uri);
                        req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        if (Content.id == 2) {
                            req.setDestinationInExternalPublicDir("/VideoDownloader", "insta.mp4");
                        } else {
                            req.setDestinationInExternalPublicDir("/VideoDownloader", "insta.jpg");
                        }
                        StyleableToast.makeText(getBaseContext(), "Download Started", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        Long ref = dm.enqueue(req);
                    }
                });
            }
        }
    }

doInBackground 从网站获取 HTML 并过滤所需的视频下载链接。这对于公共(public)链接工作正常,但对于私有(private)链接则崩溃。 现在我希望当我输入私有(private)链接时应用程序不会崩溃。非常感谢帮助 Error Image

最佳答案

既然您提到 if-else 无法检查图像路径,那么尝试使用 try - catch block 来处理异常。

try {
    Picasso.get().load(imglink).placeholder(R.drawable.loading).into(img);
} catch(Exception ex) {
    ex.printStackTrace();
    t.setVisibility(View.GONE);
    b.setVisibility(View.GONE);
}

除此之外,您还可以使用 == 运算符来比较字符串,它始终为您提供 false 。尝试使用equals()/equalsIgnoreCase()

if (dlink.equalsIgnoreCase("https://www.10insta.net/download.php?url="))

关于java - 我遇到 AsyncTask 类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58736378/

相关文章:

Java-无法将 1000 批插入表中

android - "How to retrieve data from multiple tables using cursors in android studio"

javascript - 如何将 jQuery 对象附加到文档 fragment ?

c# - 为什么我的 .NET 启动时间随着预生成的序列化程序集而增加?

performance - 如何加速 VBA 宏

java - weblogic session 的问题

java - namespace 和 JDOM

android - 使用 getRuntime().exec() 命令执行 ffmpeg 命令

java - 使用 java Supplier 接口(interface)创建条件对象

java - 等于方法: Comparing 2 Objects