如何解决图片下载线程中的错误?

时间:2016-01-05 11:44:37

标签: java android multithreading download threadpool

在我的代码中,首先我访问一个地址,然后我得到了文本文件。在那里,有许多图片链接,例如http://dnight-math.stor.sinaapp.com/%E5%9C%B0%E7%90%861_img004.jpg。我使用正则表达式来查找制作arraylist的所有链接。然后我使用downloadService下载所有图片。当我第一次按下按钮下载时,它可以成功运行。但如果再次按下按钮并抛出错误,它就不起作用。我认为这个bug是关于线程的,但我不知道如何解决它。

HttpUtil.sendHttpRequest(address,
                        new HttpCallbackListener() {

    @Override
    public void onFinish(String response) {

        try {
            ArrayList<String> urlList = new ArrayList<>();
            Pattern p = Pattern.compile("http:.*?.com/(.*?.(jpg|png))");
            Matcher m = p.matcher(response);
            StringBuffer buffer = new StringBuffer();
            while (m.find()) {
                m.appendReplacement(buffer, "<T>" +                                                 + m.group(1) + "</T>");
                urlList.add(m.group());

            }
            m.appendTail(buffer);
            response = buffer.toString();
            Message m2 = Message.obtain();
            m2.obj = response;
            m2.what = 1;
            mHandler.sendMessage(m2);
            new DownloadService("/data/data/com.baodian/files",
                                urlList,
                                new DownloadStateListener() {

            @Override
            public void onFinish() {                                                    
            }

            @Override
            public void onFailed() {
            }
        }, context).startDownload();
        ;

    // JSONObject singleChoice=all.getjson
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onError(Exception e) {
}
});

public class HttpUtil {

    public static void sendHttpRequest(final String address,
        final HttpCallbackListener listener) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            HttpURLConnection connection=null;
            try {
                URL url=new URL(address);
                connection=(HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(8000);
                connection.setReadTimeout(8000);
                connection.setDoInput(true);
                connection.setDoOutput(true);
                InputStream in=connection.getInputStream();
                BufferedReader reader=new BufferedReader(new InputStreamReader(in,"gbk"));
                StringBuilder response=new StringBuilder();
                String line=null;
                while ((line=reader.readLine())!=null) {
                    response.append(line);
                }
                if (listener!=null) {
                    listener.onFinish(response.toString());
                }
            } catch (Exception e) {
                if (listener != null) {
                    listener.onError(e);
                }
            }
        }
    }).start();
}
}

catlog

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

如果你看一下SimY4的答案here, 他说你得到的错误&#34;意味着线程池忙,队列也满了#34; 您目前所做的是在遇到错误时致电onFailed。你能做的就是实施 补充入队计划。您可以缓存较新的URL,直到线程队列有空间,创建和入队 那时的新线程。

以下主题可能有用:Java executors: how to be notified, without blocking, when a task completes?