下载Web内容-长时间下载

时间:2018-06-20 18:02:27

标签: java android regex android-asynctask

我学习Java已有几个月了。如今,我正在创建一个应用程序,该应用程序可以下载Web内容并以ListView形式显示数据。问题在于,所有操作都需要很长时间-从启动应用程序开始大约需要25秒才能填充元素列表。而且,它在真实设备或Android模拟器上运行之间没有区别。

我认为Java代码还不错,因为它大部分来自我在Udemy上的课程。

我该如何改善?问题出在哪里?

来自logcat和AVD设置[ss]的报告: Reports from logcat and AVD setting [ss]

下面是我的代码:

MainActivity.java

package com.example.user.legionisciapp;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    List<News> newsList = new ArrayList<>();
    ArrayList<String> titles = new ArrayList<>();
    ArrayList<String> desc = new ArrayList<>();
    ArrayList<String> urls = new ArrayList<>();

    public class DownloadTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {

            URL url;
            HttpURLConnection connection = null;
            String result = "";

            try {

                url = new URL(strings[0]);
                connection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = connection.getInputStream();
                InputStreamReader reader = new InputStreamReader(inputStream);

                int data = reader.read();

                while (data != -1) {

                    char current = (char) data;
                    result += current;
                    data = reader.read();

                }

                return result;

            } catch (Exception e) {

                e.printStackTrace();
                return "Failed";

            }

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DownloadTask downloadTask = new DownloadTask();

        try {

            String result = downloadTask.execute("https://www.legionisci.com").get();
            String[] resultSplit = result.split("<div id=\"mecze\">");
            result = resultSplit[0];
            resultSplit = result.split("<div id=\"listanewsow\">");
            result = resultSplit[1];

            Pattern p = Pattern.compile("class=\"b\" title=\"(.*?)\">");
            Matcher m = p.matcher(result);

            while (m.find()) {

                titles.add(m.group(1));

            }

            p = Pattern.compile("></a>(.*?)</div>");
            m = p.matcher(result);

            while (m.find()) {

                desc.add(m.group(1));

            }

            p = Pattern.compile("alt=\"\" class=\"zl\" /></a>(.*?)<");
            m = p.matcher(result);

            while (m.find()) {

                urls.add("https://www.legionisci.com" + m.group(1));

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        listView = findViewById(R.id.listView);


        for (int i = 0; i < 4; i++) {


            newsList.add(new News(R.drawable.ll, titles.get(i), desc.get(i)));


            NewsListAdapter newsListAdapter = new NewsListAdapter(this, R.layout.news_list, newsList);
            listView.setAdapter(newsListAdapter);


        }
    }
}

News.java

package com.example.user.legionisciapp;

    public class News {

        int image;

        String title, desc;

        public News(int image, String title, String desc) {
            this.image = image;
            this.title = title;
            this.desc = desc;
        }

        public int getImage() {
            return image;
        }

        public String getTitle() {
            return title;
        }

        public String getDesc() {
            return desc;
        }
    }

NewsListAdapter.class

package com.example.user.legionisciapp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class NewsListAdapter extends ArrayAdapter<News> {

    Context ctx;
    int resource;
    List<News> newsList;

    public NewsListAdapter (Context ctx, int resource, List<News> newsList){
        super(ctx, resource, newsList);

        this.ctx = ctx;
        this.resource = resource;
        this.newsList = newsList;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(ctx);

        View view = inflater.inflate(R.layout.news_list, null);

        TextView title = view.findViewById(R.id.tvTitle);
        TextView desc = view.findViewById(R.id.tvDesc);
        ImageView image = view.findViewById(R.id.thumb);

        News news = newsList.get(position);

        title.setText(news.getTitle());
        desc.setText(news.getDesc());
        image.setImageDrawable(ctx.getResources().getDrawable(news.getImage()));

        return view;

    }
}

1 个答案:

答案 0 :(得分:0)

不能执行以下操作:

String result = downloadTask.execute("https://www.legionisci.com").get();

那不是使用AsyncTask的正确方法!问题是异步任务是在后台线程中执行的,但是如果您执行.get(),则主线程将暂停,直到异步任务完成其工作为止。因此,这就是启动等待时间长的原因。

您要做的是:

以这种方式启动任务

downloadTask.execute("https://www.legionisci.com");

使用onPostExecute(String result)方法(在主线程上执行)获取结果并更新UI。

有关此的更多信息: https://developer.android.com/reference/android/os/AsyncTask