gpt4 book ai didi

java - 我遇到 AsyncTask 类的问题

转载 作者:行者123 更新时间:2023-12-02 09:20:19 26 4
gpt4 key购买 nike

当链接为空时,我的应用程序崩溃。我正在尝试下载 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/

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