我是Android开发的初学者。我正在尝试学习多线程并与Internet一起工作,因此我通过使用AsyncTask
通过后台线程从链接下载PDF文件来做到这一点。我对最好的解决方法感到困惑。
我是否在URI
类的onCreate
或doInBackground
方法中创建AsyncTask
和其他必要的连接对象?
要仅下载一个PDF文件,我需要调用哪种对象?
我已经检查了文档,但我不太了解。我很感谢外行的解释,甚至可能是伪代码。
这是我到目前为止的代码:
public class MainActivity extends AppCompatActivity {
Button downloadPDF;
DownloadingClass downPDF;
private static final String TAG = "omar.asynctaskdemo;";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String urlExample = "https://doc.lagout.org/programmation/Actionscript%20-%20Flash%20-%20Flex%20-%20Air/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29.pdf");
downloadPDF = findViewById(R.id.download_pdf);
downPDF = new DownloadingClass();
downloadPDF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downPDF.execute();
}
});
}
private class DownloadingClass extends AsyncTask<String, Integer, Void>{
@Override
protected Void doInBackground(String... strings) {
return null;
}
}
}
答案 0 :(得分:1)
AsyncTask只是在后台线程中运行您的代码。下载pdf文件就像其他任何文件一样。
您将需要使用$("div#myId").dropzone({
url: "/stores/sell_upload/",
maxFiles: 4,
acceptedFiles: "image/*",
addRemoveLinks: true,
renameFile: function(file) {
let newName = file.name.replace("." + file.name.split(".").pop(), "") + "-" + new Date().getTime() + "." + file.name.split(".").pop();
return newName;
},
removedfile: function(file) {
$.ajax({
type: "POST",
url: "/stores/sell_upload/",
data: {
name: file.name,
action: "delete"
}
});
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
}
});
,创建一个String constr = (@"Data source=(localdb)\MSSQLLocalDB; initial catalog=DataHRD; integrated security=true;");
// Create a new SqlConnectionStringBuilder and
// initialize it with a few name/value pairs.
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(constr);
// Now that the connection string has been parsed,
// you can work with individual items.
builder["Data Source"] = @"C:\Users\SONY\Desktop\asda\asd\MainMenu\DataHRD.mdf"
// or anything else
builder.Password = "new@1Password";
builder.AsynchronousProcessing = true;
// You can refer to connection keys using strings,
// as well. When you use this technique (the default
// Item property in Visual Basic, or the indexer in C#),
// you can specify any synonym for the connection string key
// name.
builder["Server"] = ".";
builder["Connect Timeout"] = 1000;
builder["Trusted_Connection"] = true;
并编写HttpUrlConnection
。请参阅this
让上述代码在AsyncTask类的FileOutputStream
中执行,并更好地在构造函数中传递url并在inputstream
方法内进行所有操作。因为您不想阻止UI线程。