File Download On Button Click?
Solution 1:
You can't place one function inside another function
publicvoidDownload(View Button) {
publicvoidDownloadFromUrl(){
Solution 2:
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):
publicclassDownloadExampleActivityextendsActivity {
/** Called when the activity is first created. */
Button button;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.download_button);
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
DownloadFromUrl();
}
});
}
publicvoidDownloadFromUrl() {
try {
URLurl=newURL("www.generic-site.html");
HttpURLConnectionc= (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
StringPath= Environment.getExternalStorageDirectory() + "/download/";
Log.v("PortfolioManger", "PATH: " + Path);
Filefile=newFile(Path);
file.mkdirs();
FileOutputStreamfos=newFileOutputStream("site.html");
InputStreamis= c.getInputStream();
byte[] buffer = newbyte[702];
intlen1=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: ");
}
}
Solution 3:
@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.javapublicclassA
{
publicclassB
{}//inner class B
}//class AclassC
{}//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
publicclassA
{
//data memberinta=0;
//other data member, static and private, why notprivatestaticStringb="toto";
//methodsprivatevoidmet( 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
Post a Comment for "File Download On Button Click?"