luke
luke

Reputation: 21

File download on button click?

So im new to android development and i have been going about everything in a trial and error manner with a lot of searching a long the way. My question is: I have a button that I want to link to a exert of code that will start a download from a particular site. my code is as follows.

    public void Download(View Button) {
    public void DownloadFromUrl(){
        try {

            URL url = new URL("www.generic-site.html");
            HttpURLConnection c = (HttpURLConnection) 
            url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String Path = Environment.getExternalStorageDirectory()
                +"/download/";
            Log.v("PortfolioManger", "PATH: "+Path);
            File file = new File(Path);
            file.mkdirs();
            FileOutputStream fos = new FileOutputStream("site.html");

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[702];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
        } catch (IOException e) {
            Log.d("PortfolioManger", "Error: "+e);
        }
        Log.v("PortfolioManger", "Check: ");
    }

What I was attempting to do was use the "public void Download(View Button)" command to launch the download, however im getting the errors:

Multiple markers at this line
- Syntax error, insert "EnumBody" to complete BlockStatements
- Syntax error on token "void", @ expected
- Syntax error, insert "enum Identifier" to complete 
 EnumHeaderName" error under "Public void DownloadFromUrl()

I know its probably a silly question but can anyone shed some light?

Upvotes: 2

Views: 10200

Answers (3)

Caspar Harmer
Caspar Harmer

Reputation: 8117

You really need to sort out your Java syntax, but for now, this will work, assuming that you put in a correct url (I was not able to test as you used a demo url):

public class DownloadExampleActivity extends Activity {
    /** Called when the activity is first created. */

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.download_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadFromUrl();
            }
        });
    }

    public void DownloadFromUrl() {
        try {

            URL url = new URL("www.generic-site.html");
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String Path = Environment.getExternalStorageDirectory() + "/download/";
            Log.v("PortfolioManger", "PATH: " + Path);
            File file = new File(Path);
            file.mkdirs();
            FileOutputStream fos = new FileOutputStream("site.html");

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[702];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();
        } catch (IOException e) {
            Log.d("PortfolioManger", "Error: " + e);
        }
        Log.v("PortfolioManger", "Check: ");
    }

}

Upvotes: 4

Snicolas
Snicolas

Reputation: 38168

@mibollma is right, you are actually not respecting the structure of classes in java.

A class file in java - must contain one and only one public class - the name of this class and the name of the file must match

there can be others classes, but either not public or inner classes like

//in A.java
public class A
{
  public class B 
  {}//inner class B

}//class A

class C
{}//class C

In a class, you are allowed to use - data member definitions - inner class definitions (see above, so yes this structure is recursive / fractal) - methods

like in

public class A
{
   //data member
   int a = 0;
   //other data member, static and private, why not
   private static String b = "toto";

   //methods
   private void met( int b ) 
   {}//met

   //...
}//class A

This is the big picture. And inside a method you can't add anything but instructions. No method nesting is allowed. Note that those examples don't talk about anonymous inner classes, it's a bit more advanced.

Please also take some time to review java naming conventions, your code doesn't respect naming standards, it's harder to follow.

Regards, stéphane

Upvotes: 1

mibollma
mibollma

Reputation: 15118

You can't place one function inside another function

 public void Download(View Button) {
 public void DownloadFromUrl(){

Upvotes: 5

Related Questions