Android 使用jsoup和asynctask使用下载管理器下载多个文件,不发生

Android 使用jsoup和asynctask使用下载管理器下载多个文件,不发生,android,android-asynctask,jsoup,Android,Android Asynctask,Jsoup,这是我的密码:- public class grabber extends Activity { int counter; String folderPath; String[] array; DownloadManager dm; private long enqueue; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVi

这是我的密码:-

public class grabber extends Activity 
{


int counter;
String folderPath;
String[] array;
DownloadManager dm;
private long enqueue;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);

    Down t = new Down();
    t.execute();




}


private class Down extends AsyncTask<String, Void, String> 
{
    protected String doInBackground(String... params)
    {
        Log.i("grabber", "in back");
String   w =Environment.getExternalStorageDirectory().getAbsoluteFile()+"/Android/data/myfolder";
    createdir(w);


    String link = "http://mywebsitehere.com";


    Document doc;

      array = new String[200];
    try {

        doc = Jsoup.connect(link).get();
        String title = doc.title();


         folderPath = w+ File.separator+title;

         createdir(folderPath);


        Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
         counter =0;

        for (Element image : images) 
        {
             String img = image.attr("src");
            array[counter]=img;

            counter++;
        }

    } catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}


    protected void onPostExecute(String result) 
    {   

        Toast.makeText(getApplicationContext(), 
          "done", Toast.LENGTH_SHORT).show();

        for(int i=0;i<counter;i++)
        {
            dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            Request request = new Request(
                    Uri.parse(array[i]));
            request.setDescription("Android Data download using DownloadManager.");
              request.setDestinationInExternalFilesDir(getApplicationContext(),folderPath,File.separator+"pic"+Integer.toString(i)+".jpg");
            enqueue = dm.enqueue(request);



        }


    }
 }




public void createdir(String Path)
{
File file = new File(Path);
    if(!file.exists())
    {
        file.mkdirs();

    }

}
}
它应该做什么:- 它应该将包含图像的给定链接下载到给定路径

它的作用是:- 应用程序运行得很好,我的UI没有挂起。几秒钟后,会显示Toast,这意味着同步任务已完成,我们处于onPostExecute。但是当我检查dir时,我发现Android/data/myfolder中没有文件夹。 主要的挫折是它有时能工作,即图像被下载,有时不能

所以我猜这可能是因为http超时或者我使用createdirfolderPath时没有正确处理的错误;在Jsoup之后,可能会发生错误,因此不会在那里创建文件夹

有什么想法吗!??提前谢谢

编辑:-
我在jsoup.connectlink.get期间收到SocketTimeoutException

您必须将以下权限添加到应用程序的清单中,才能写入外部存储

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我希望是你干的

最后我做了一些尝试和错误

我们可以在这里使用timeout选项,通过更改

        doc = Jsoup.connect(link).get();

还有一个错误,那就是删除onPostExecute中的Toast消息,因为没有处理程序,我们无法从后台任务到UI进行交互


就这样。

是的。。如果我试了10次,有一次我运气好,下载就发生了。你有例外吗?您还可以将try{}catch{}添加到onPostExecute方法以记录任何错误。问题是在jsoup.connectlink.get期间,当我获取sockettimeoutexception时
        doc = Jsoup.connect(link).timeout(10000).get();