【发布时间】:2019-11-06 18:23:59
【问题描述】:
当链接为空时,我的应用程序崩溃。我正在尝试下载 Instagram 视频。这些应用程序可以与公共链接一起正常工作。但是我放了一个私人链接,应用程序崩溃了。这意味着通过私有链接,应用程序不会从网站获取 HTML。并且下载链接仍然是空的。这会导致应用程序崩溃。我希望在输入私人链接时应用程序不会崩溃
我尝试在 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 并过滤所需的视频下载链接。这适用于公共链接,但会导致私有链接崩溃。 现在我希望当我进入私人链接时应用程序不会崩溃。非常感谢您的帮助 Error Image
【问题讨论】:
-
因为您的
imglink为 null 或为空,请确保您将有效路径传递给 picasso 加载程序。 -
我已经知道它是空的,这就是应用程序崩溃的原因。告诉我一个防止它的方法
-
简单的 if 块会阻止这个 if(validPath(imglink)){ Picasso.get().load(imglink).placeholder(R.drawable.loading).into(img); }
-
你没有明白我的意思
-
私人链接需要身份验证或令牌。您需要检查 instagram API(如果可用,我不知道)并检查您需要如何使用正确的身份验证执行所需的请求
标签: java android performance android-asynctask jsoup