开发者

Download button does not work please help

开发者 https://www.devze.com 2023-01-20 12:58 出处:网络
Ok so I have my app 90% built everything works but my download button. I have been stuck on this for days now so if anyone could help a guy out with something that will work for this. my files are .pk

Ok so I have my app 90% built everything works but my download button. I have been stuck on this for days now so if anyone could help a guy out with something that will work for this. my files are .pkg and I feed the filename from a xml file and here is my buttons code but I dont know what I am doing wrong here. It just does nothing upon clicking it. The appUrl is my file name from the xml file

Button b_get_book = (Button)v.findViewById(R.id.Button01);
        b_g开发者_如何学编程et_book.setOnClickListener(
                new View.OnClickListener()
                {              
                    public void onClick(View v) {
                         try {
                                File root = Environment.getExternalStorageDirectory();
                                URL u = new URL("http://www.mysite.com/" + appUrl);
                                HttpURLConnection c = (HttpURLConnection) u.openConnection();
                                c.setRequestMethod("GET");
                                c.setDoOutput(true);
                                c.connect();
                                FileOutputStream f = new FileOutputStream(new File(root, appUrl));

                                InputStream in = c.getInputStream();

                                byte[] buffer = new byte[1024];
                                int len1 = 0;
                                while ((len1 = in.read(buffer)) > 0) {
                                    f.write(buffer, 0, len1);
                                }
                                f.close();
                            } catch (Exception e) {
                                Log.d("Downloader", e.getMessage());
                            }  
                    }
           });

Any help would be great I have been pulling my hair out over this. I make a pretty complex app and its the easy thing that gets me...... go figure. if anyone can help me out with some working code from a button that would be awesome. even better if it has a progress bar for the downloads. that is my next task after getting the actual download working. Thanks


First idea is that you get an IOException when trying to create a file. If I were you I would, first of all, move the code which is currently in your try-catch clause to somewhere else, and test it. Then when it's working well I would just call it from within try-catch.

Also, I would recommend against running such long operations in GUI thread - that makes your app non-responsive to user input. The following approach is most recommended:

        public void onClick(View v) {
            new AsyncTask<Void, Void, Void>() {
                  @Override
                  protected Void doInBackground(Void... params) {
                      try {
                        /* your code here*/
                      } catch (Exception e) {
                        Log.e("Downloader", e.getMessage());
                      }  
                  }
               }.execute();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消