-2

In my code,first I access an address and I got the text file. In that, there are many picture links, such as http://dnight-math.stor.sinaapp.com/%E5%9C%B0%E7%90%861_img004.jpg. I use regular expression to find all the links to make a arraylist. Then I use downloadService to download all the pictures. When I first press a button to download ,it can run successfully. But it doesn't work if the button is pressed again and throws error. I think this bug is about thread but I don't know how to solve it.

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

Paul Wang
  • 173
  • 6

1 Answers1

0

If you look at SimY4's answer here, he says that the error you're getting "means the thread pool is busy and queue is full as well". What you currently do is call onFailed when you encounter the error. What you can do is implement a supplementary enqueing scheme. You can cache the newer urls until the thread queue has space, create and enqueue the new threads at that point.

The following thread might prove useful : Java executors: how to be notified, without blocking, when a task completes?

Community
  • 1
  • 1
Adeeb
  • 1,271
  • 3
  • 16
  • 33