我一直在使用AsyncTask下载某个文件,并经历了一些教程,只是未能使进度条随着下载而移动。代码是和 AsyncTask 调用一个方法来执行 HTTP 连接,然后返回以正确的方式对数据进行排序,以便为应用程序操作它
这是我的AsynTask,在主要活动
private class getFood extends AsyncTask<Void, Integer, Cursor> {
private ProgressDialog mProgressDialog;
@Override
protected Cursor doInBackground(Void... params) {
// Create URL object
String site = "https://afternoon-ridge-50060.herokuapp.com/allsnacks";
URL url = createUrl(site);
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = null;
try {
String jsonResponseEmpty = "";
// If the URL is null, then return early.
if (url == null) {
jsonResponse = jsonResponseEmpty;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(20000 /* milliseconds */);
urlConnection.setConnectTimeout(25000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Authorization", "\"token\": " + token);
urlConnection.connect();
达令说
暮色呼如
相关分类