0
public void downdown(View view) {
    try {

        URL url = new URL("http://myserver.com:7005/media/databases/myfile.db");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        File SDCardRoot = Environment.getExternalStorageDirectory();
        File file = new File(SDCardRoot,"myfile.db");
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        int totalSize = urlConnection.getContentLength();
        int downloadedSize = 0;
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {

            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;
            //actualizaProgreso(downloadedSize, totalSize);
        }
        fileOutput.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.downloader"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Error:

When i press the button download (this button calls downdown function: this function downloads a file from a server) i get the following error: java.lang.IllegalStateException: Could not execute method of the activity

1smae1
  • 11
  • 3

2 Answers2

0

You cannot have network actions within the main thread.

Look into AsyncTask and multi-Threading. http://developer.android.com/reference/android/os/AsyncTask.html

Android : Loading an image from the Web with Asynctask

If you really want to improve you skills I suggest not using an instance of thread to start your process. I suggest using ExecutorService

Here is a guide to how to use http://tutorials.jenkov.com/java-util-concurrent/scheduledexecutorservice.html

Community
  • 1
  • 1
Ya Wang
  • 1,758
  • 1
  • 19
  • 41
0
public void downdown(View view) {
    new Thread() {
              public void run() {
                 downdown();
              }
      }.start();
   }
   public void downdown(){
 try {

    URL url = new URL("http://myserver.com:7005/media/databases/myfile.db");
    HttpURLConnection urlConnection = (HttpURLConnection)      url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();
    File SDCardRoot = Environment.getExternalStorageDirectory();
    File file = new File(SDCardRoot,"myfile.db");
    FileOutputStream fileOutput = new FileOutputStream(file);
    InputStream inputStream = urlConnection.getInputStream();
    int totalSize = urlConnection.getContentLength();
    int downloadedSize = 0;
    byte[] buffer = new byte[1024];
    int bufferLength = 0;
    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {

        fileOutput.write(buffer, 0, bufferLength);
        downloadedSize += bufferLength;
        //actualizaProgreso(downloadedSize, totalSize);
    }
    fileOutput.close();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}